nis
nis

Reputation: 11

Node js natural sort on array of objects based on string and numeric key values

I have an array of object with string and numeric values. Need a natural sort to work on combined values. Example:

input array:

[
{ 
    key1: '156557_08_315F036D',
    key2: 30,
    key3: 's' },
  { 
    key1: '156557_08_315F036D',
    key2: 10,
    key3: 'm' },
 { 
    key1: '156557_08_315F036D',
    key2: 10,
    key3: 's' },
  { 
    key1: '156557_08_315F036D',
    key2: 15,
    key3: 's' },
  { 
    key1: '156557_08_315F036D',
    key2: 20,
    key3: 's' }
]

This should be sorted in the ascending order of key3+key1+key2 format, where only key2 is numeric and wants to be sorted naturally..not as string.

Output would be:

[
{ 
    key1: '156557_08_315F036D',
    key2: 10,
    key3: 'm' },
{ 
    key1: '156557_08_315F036D',
    key2: 10,
    key3: 's' },
{ 
    key1: '156557_08_315F036D',
    key2: 15,
    key3: 's' },
{ 
    key1: '156557_08_315F036D',
    key2: 20,
    key3: 's' },
{ 
    key1: '156557_08_315F036D',
    key2: 30,
    key3: 's' }
]

arr.sort(function (a, b) {
    return (
      a.key3 + a.key1 + parseInt(a.key2) >
      b.key3 + b.key1 +  parseInt(b.key2) ?
      1 :
      ((b.key3 + b.key1 +  parseInt(b.key2) >
        a.key3 + a.key1 +  parseInt(a.key2)) ?
      -1 : 0));
  });

Tried toString of key2, but did string sort.

Upvotes: 0

Views: 985

Answers (3)

nis
nis

Reputation: 11

I used Lodash:

const sorted = _.orderBy(arr, ['key3', 'key1', 'key2'], ['asc', 'asc', 'asc']);

It is awesome!

Upvotes: 0

Venu Dasarathy
Venu Dasarathy

Reputation: 26

I misread your question, you can use the === operator. This won't auto convert types like the '==' operator. I'm not sure what you are trying to do though, it looks like you want to the sort criteria to be based on some order of attributes. If so, below code would work.

function compareFunction(obj1,obj2){
    var ord = ['key3','key1','key2']; //Specify order here 
    for(var i =0;i<ord.length;i++){
        if(obj1[ord[i]] === obj2[ord[i]]){
            continue;
        }
        else{
            if(obj1[ord[i]] < obj2[ord[i]]){
                return -1;
            }
            if(obj1[ord[i]] > obj2[ord[i]]){
                return 1;
            }
        }
    }
return 0;
}

Then simply call arr.sort(compareFunction);

Upvotes: 0

Nick Bull
Nick Bull

Reputation: 9866

If you don't mind an extra dependency, here's a cool library called thenBy:

// first by length of name, then by population, then by ID
data.sort(
    firstBy(function (v) { return v.name.length; })
    .thenBy("population")
    .thenBy("id")
);

This sorts by using a function, or by the key name of the object property.

Upvotes: 1

Related Questions