0

i want to sort a list of objects alphabetically based on another list of characters and the property 'name'

this is the sorting list and the order that i wish to follow in order to sort my list:

 const SortingArray = [['a','á','à','â','ä','å','ã'],['b'],['c','ç'],['d'],
                      ['e','é','è','ê','ë'],['f'],['g'],['h'],['i','í','ì','î','ï'],
                      ['j'],['k'],['l'],['m'],['n','ñ'],['o','ó','ò','ô','ö','õ'],['p'],
                      ['q'],['r'],['s','š'],['t'],['u','ú','ù','û','ü'],
                      ['v'],['w'],['x'],['y','ý','ÿ'],['z','ž']];

and this is the list that i want to sort :

var listToSort = [{name: 'Étape',code: 'c12'},{name: 'Abc',code: 'c14'},{name: 'Ûrter',code: 'c15'}
]

1 Answer 1

2

You can do it by implementing a sort function that calculates the index of the first character of the words in the SortingArray like this:

    listToSort.sort((a, b) => {
        const aIndex = SortingArray.findIndex(charArray => 
            charArray.includes(a.name[0].toLowerCase())
        );
        const bIndex = SortingArray.findIndex(charArray => 
            charArray.includes(b.name[0].toLowerCase())
        );
        return aIndex - bIndex;
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.