1

I have an array of objects as below and need to sort it in such fashion that items with c comes first, then y , then s. And in that too, for e.g. in c, it should be sorted by counter i.e. c- counter 1, c- counter 2.

var data = [ { age:7, counter: 1, code: 'c'},
     { age:5, counter: 2, code: 'c'},
     { age:4, counter: 3, code: 'c'},
     { age:19, counter: 2, code: 'y'},
     { age:22, counter: 1, code: 'y'},
     { age:57, counter: 1, code: 's'},
     { age:80, counter: 2, code: 's'}
    ]

Upon looking on SO, I was able to sort by c, y, s as below, but could not do another sort for counter inside. How can I do that.

var order =[c,y,s];
data.sort(function(a,b){
        return order.indexOf(a.code) < order.indexOf(b.code) ? -1:1;
})

4 Answers 4

3

You could use an object for the order of code and then sort by count.

var data = [{ age: 7, counter: 1, code: 'c' }, { age: 5, counter: 2, code: 'c' }, { age: 4, counter: 3, code: 'c' }, { age: 19, counter: 2, code: 'y' }, { age: 22, counter: 1, code: 'y' }, { age: 57, counter: 1, code: 's' }, { age: 80, counter: 2, code: 's' }];

data.sort(function (a, b) {
    var order = { c: 1, y: 2, s: 3 };
    return order[a.code] - order[b.code] || a.counter - b.counter;
});

console.log(data);

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

3 Comments

I cant put counter in object.
@whyAto8, what does it mean?
Apologies, I mistook the code and hence wrote that. Please ignore that.
1

You need to specify array as

var order =['c', 'y', 's'];

otherwise c, y and s needs to be variables.

Also, you need to compare the counter variables as well if codes are equal

data.sort(function(a,b){
   if ( a.code == b.code )
   {
        return a.counter - b.counter;
   }
   else
   {
       return order.indexOf(a.code) - order.indexOf(b.code);
   }
})

Comments

0

This is a simple way to sort by counter:

var order = [ 'c', 'y', 's' ];

data.sort(function (a,b) {
    var o;

    if (order.indexOf(a.code) === order.indexOf(b.code))
    {
        o = a.counter - b.counter;
    } else {
        o = order.indexOf(a.code) - order.indexOf(b.code);
    }

    return o;
});

Comments

0

This is a dynamic way which allows you to add your conditions to be compare one after another.

var data = [{ age: 7, counter: 1, code: 'c' }, { age: 5, counter: 2, code: 'c' }, { age: 4, counter: 3, code: 'c' }, { age: 19, counter: 2, code: 'y' }, { age: 22, counter: 1, code: 'y' }, { age: 57, counter: 1, code: 's' }, { age: 80, counter: 2, code: 's' }];

Array.prototype.orderBy = function(...expresions) {
  var arr = this;
  return this.sort((a, b) => {
    var result;
    expresions.every(ex => (result = ex(a) - ex(b)) === 0)
    return result;
  });
}

var codeOrder = {
  c: 1,
  y: 2,
  s: 3
};
var result = data
  .orderBy(i => codeOrder[i.code], i => i.counter)
console.log(result);

How it works

The usage is simple:

dataArray.sortBy(ex1[, ex2[, ex3]])

every arguments of this method is a function which accepts one item from dataArray as input and returns a number related to that item. if any of expressions return same value with respect to adjacent item then evaluation continues with next expression on item and adjecent until they return different result or there is no other expression to test.

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.