I have an array of objects that I need to sort in a specific manner. First, I want items with the code 'c' to come first, followed by 'y', and then 's'. Within each group (c, y, s), they should be sorted based on the counter values. For example, for code 'c', it should be sorted by the 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'}
]
After searching on SO, I was able to sort by 'c', 'y', 's' using the following code snippet. However, I'm stuck at sorting by the 'counter' values within each category. Can anyone help me achieve this nested sorting?
var order = ['c','y','s'];
data.sort(function(a,b){
return order.indexOf(a.code) < order.indexOf(b.code) ? -1 : 1;
})