0

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.

2
  • OK. Where is your code? Commented Aug 8, 2016 at 11:37
  • added to question @str Commented Aug 8, 2016 at 11:51

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

I used Lodash:

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

It is awesome!

1 Comment

Beware: lodash doesn't do natural order sort.

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.