sort an array of objects on multiple key using array-sort

Sadiq Sikkandhar

I have a requirement where the array of objects needs to be sorted on certain keys. The keys with which it needs to be sorted is dynamic and it is Not fixed. I came across array-sort in npm library. Using that, am able to sort on multiple keys but it sorts only on ascending order.

const input = [{id:'1',name:'John',city:'Denver',State:'CO'},
               {id:'2',name:'Smith',city:'San Fransisco',State:'CA'},
               {id:'3',name:'Adam',city:'Concord',State:'CA'},
               {id:'1',name:'John',city:'Concord',State:'CA'}]

I want to sort on State (asc), city (asc) and id (desc). My output should look like

[
 {id:'3',name:'Adam',city:'Concord',State:'CA'},
 {id:'1',name:'John',city:'Concord',State:'CA'},
 {id:'2',name:'Smith',city:'San Fransisco',State:'CA'}, 
 {id:'1',name:'John',city:'Denver',State:'CO'}]

Can anyone please let me know how i can implement sorting on descending using array-sort

Thanks

Quciksilver

Maybe you want a JavaScript function like this?

function multicolumnSort(data, orders) {
    return data.sort((e1, e2) => {
        for (var i = 0; i < orders.length; i++)
            if (e1[orders[i].column] != e2[orders[i].column])
                return orders[i].desc ^ e2[orders[i].column] < e1[orders[i].column]? 1 : -1;
        return 0;
    });
}

Then, you may call the function with your order keys:

let orders = [
    {
        column: 'State'
    },
    {
        column: 'city'
    },
    {
        column: 'id',
        desc: true
    }
];

let result = multicolumnSort(input, orders);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sort array of objects by single key with date value

How to sort an array of objects by multiple fields?

Swift - Sort array of objects with multiple criteria

How to sort an array of objects based on nested key using Ramda?

Sort Array of Objects by specific key value | Javascript

Sort objects in array by key with lodash

Javascript sort an array of objects based on numeric key

order or sort array of objects based on a key value

Sort Array of Objects by Nested Key Value

Sort Array of Nested Objects by Dynamic Key

Sort by multiple objects in a array in Javascript

Sort fixed array of objects by using another array

Javascript sort array of objects using array of priority

Sort an array of objects by multiple properties

Using a selection sort to sort an array of objects?

Sort array of objects following multiple rules in javascript

sort array of multiple inheritance objects by date

Sort an array of objects by the date where the date is the key

Javascript sort array of objects on key

Sort Javascript array containing objects by key value

Sort array of objects by a key

Sort Array of Key-Value Paired Objects using an Array of Arrays

Sort array of objects using multiple date properties

Sort an array of objects based on key/value of array

sort array by multiple keys with optional key

How to sort an array of objects by multiple date fields?

Sort array of objects based on another array of objects key

Sort an array of objects by multiple fields

Sort array of objects by multiple fields using javascript sort()