Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort array of object with another array of strings

I’m creating a chat with firestore.
I want to sort a array based on another array that has strings that are represented by the uid present on the first array.

first I have this state and this ID.

const [allInp, setAllInp] = useState({ searchConv: '' })
const dropId = searchConv

I have this array

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const findUser = (ele) => {
    if (!ele || !ele.fullName) return false
    return ele.fullName.toLowerCase().includes(allInp[dropId].toLowerCase())
}
const myUid = 'ibPlTju8K2d3PlydP6sdm2T4S2L2'
const allChat = const allChat = [
    {
        senders: [
            'GiVtXApvumQgkUpkTuOGoKbe9ZE2',
            'glBNAhj3RrSj5FBDwOzzSnPKN5S2'
        ],
        lastTalk: {
            seconds: 1666016615,
            nanoseconds: 242000000
        },
        lastMessage: 'lorem',
        glBNAhj3RrSj5FBDwOzzSnPKN5S2: {
            uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
            userRead: true,
            fullName: 'Carina Lima'
        },
        docid: 'GiVtXApvumQgkUpkTuOGoKbe9ZE27777glBNAhj3RrSj5FBDwOzzSnPKN5S2',
        GiVtXApvumQgkUpkTuOGoKbe9ZE2: {
            userRead: false,
            uid: 'GiVtXApvumQgkUpkTuOGoKbe9ZE2',
            fullName: 'Cristian Derick'
        },
        id: 'GiVtXApvumQgkUpkTuOGoKbe9ZE27777glBNAhj3RrSj5FBDwOzzSnPKN5S2'
    },
    {
        docid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777WBZHUtoZ9DhddVmS07RL0wlvDgz1',
        WBZHUtoZ9DhddVmS07RL0wlvDgz1: {
            fullName: 'Bruce Dikinson',
            uid: 'WBZHUtoZ9DhddVmS07RL0wlvDgz1',
            userRead: false
        },
        lastTalk: {
            seconds: 1665066226,
            nanoseconds: 646000000
        },
        lastMessage: 'whatever whatever',
        glBNAhj3RrSj5FBDwOzzSnPKN5S2: {
            uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
            userRead: true,
            fullName: 'Carina Lima'
        },
        senders: [
            'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
            'WBZHUtoZ9DhddVmS07RL0wlvDgz1'
        ],
        id: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777WBZHUtoZ9DhddVmS07RL0wlvDgz1'
    },
    {
        lastTalk: {
            seconds: 1666189405,
            nanoseconds: 328000000
        },
        senders: [
            'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
            'glBNAhj3RrSj5FBDwOzzSnPKN5S2'
        ],
        glBNAhj3RrSj5FBDwOzzSnPKN5S2: {
            userRead: false,
            fullName: 'Carina Lima',
            uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2'
        },
        docid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777glBNAhj3RrSj5FBDwOzzSnPKN5S2',
        lastMessage: 'whatever',
        id: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777glBNAhj3RrSj5FBDwOzzSnPKN5S2'
    },
    {
        docid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777ibPlTju8K2d3PlydP6sdm2T4S2L2',
        lastTalk: {
            seconds: 1666270072,
            nanoseconds: 99000000
        },
        lastMessage: 'Okay',
        senders: [
            'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
            'ibPlTju8K2d3PlydP6sdm2T4S2L2'
        ],
        ibPlTju8K2d3PlydP6sdm2T4S2L2: {
            uid: 'ibPlTju8K2d3PlydP6sdm2T4S2L2',
            userRead: true,
            fullName: 'Wanderson Santos'
        },
        glBNAhj3RrSj5FBDwOzzSnPKN5S2: {
            userRead: false,
            fullName: 'Carina Lima',
            uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2'
        },
        id: 'glBNAhj3RrSj5FBDwOzzSnPKN5S27777ibPlTju8K2d3PlydP6sdm2T4S2L2'
    }
]

I’m cleaning it like this to have only the data of the person I’m talking to, and alread sorting this way.

const fRes = allChat.map(ele => {
    if (!ele || !ele.senders || !ele.senders.includes(myUid)) return null
    const [newok] = ele.senders.filter(kv => kv !== myUid)
    let finalUser = newok
    if (ele.senders.filter(cv => cv === myUid).length === 2) finalUser = myUid
    return {
        ...ele[finalUser],
        lastMessage: ele.lastMessage,
        lastTalk: ele.lastTalk.toDate && new Date(ele.lastTalk.toDate()).getTime()
    }
}).filter(kk => kk).filter(wv => findUser(wv)).sort((a, b) => b.lastTalk - a.lastTalk)

response:

fRes = [
    {
        fullName: 'Wanderson Santos',
        uid: 'ibPlTju8K2d3PlydP6sdm2T4S2L2',
        userRead: true,
        lastMessage: 'okay',
        lastTalk: 1666270072099
    },
    {
        uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
        fullName: 'Carina Lima',
        userRead: false,
        lastMessage: 'whatever',
        lastTalk: 1666189405328
    },
    {
        uid: 'GiVtXApvumQgkUpkTuOGoKbe9ZE2',
        userRead: false,
        fullName: 'Cristian Derick',
        lastMessage: 'lorem',
        lastTalk: 1666016615242
    },
    {
        uid: 'WBZHUtoZ9DhddVmS07RL0wlvDgz1',
        userRead: false,
        fullName: 'Bruce Dikinson',
        lastMessage: 'whatever whatever',
        lastTalk: 1665066226646
    }
]

Now when I insert uid string on this array

const priority = [
    'WBZHUtoZ9DhddVmS07RL0wlvDgz1',
    'GiVtXApvumQgkUpkTuOGoKbe9ZE2'
]

I want a new sort based on priority, should be:

[
    {
        uid: 'GiVtXApvumQgkUpkTuOGoKbe9ZE2',
        userRead: false,
        fullName: 'Cristian Derick',
        lastMessage: 'lorem',
        lastTalk: 1666016615242
    },
    {
        uid: 'WBZHUtoZ9DhddVmS07RL0wlvDgz1',
        userRead: false,
        fullName: 'Bruce Dikinson',
        lastMessage: 'whatever whatever',
        lastTalk: 1665066226646
    },
    {
        fullName: 'Wanderson Santos',
        uid: 'ibPlTju8K2d3PlydP6sdm2T4S2L2',
        userRead: true,
        lastMessage: 'okay',
        lastTalk: 1666270072099
    },
    {
        uid: 'glBNAhj3RrSj5FBDwOzzSnPKN5S2',
        fullName: 'Carina Lima',
        userRead: false,
        lastMessage: 'whatever',
        lastTalk: 1666189405328
    }
]

Based on Last Talk and Priority
sorted by priority on top
and the rest of the users then

>Solution :

.sort((a, b) => { 
  const aPriority = priority.indexOf(a.uid)
  const bPriority = priority.indexOf(b.uid)
  if (aPriority !== -1 && bPriority !== -1) {
     return bPriority - aPriority
  }

  if (aPriority !== -1) {
     return -1
  }

  if (bPriority !== -1) {
     return 1
  }

  return b.lastTalk - a.lastTalk
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading