2

I have an array of objects. For eg-

[{
    aKey:2, 
    bKey:2, 
    cKey:3
}, { 
    bKey:2, 
    cKey:6
}, {
    aKey:1,
    bKey:6, 
    cKey:5
}, {
    bKey:1, 
    cKey:4
}, {
    bKey:6, 
    cKey:7
}]

So what I need to do is-

  1. First sort the array on the basis of aKey (asc order) and the objects which are having this key would be at the beginning in result array.
  2. Then I need to sort the array based on the value of bKey. for eg, all the records having bKey = 2, would be at the beginning.
  3. Rest records would be sorted based on the value of cKey in asc order.

So the output will be-

[{
    aKey:1,
    bKey:6, 
    cKey:5
}, {
    aKey:2, 
    bKey:2, 
    cKey:3
}, { 
    bKey:2, 
    cKey:6
}, {
    bKey:1, 
    cKey:4
}, {
    bKey:6, 
    cKey:7
}]
5
  • Do you have a question? Commented Nov 4, 2016 at 16:34
  • @JLRishe: how to sort the array of object like I explained? first object is input and second one should be the output. Commented Nov 4, 2016 at 16:36
  • 1
    Why is bKey:1 following bKey:2 at expected output? Sort is based on cKey? Commented Nov 4, 2016 at 16:40
  • @guest271314: because I need to arrange the array in that way, objects having bKey = 2, would be the first. Commented Nov 4, 2016 at 16:42
  • What issue are you having sorting the array? Commented Nov 4, 2016 at 16:47

2 Answers 2

2

For sorting preferentially from aKey to bKey and then to cKey, you can use this:

var array=[{aKey:2,bKey:2,cKey:3},{bKey:2,cKey:6},{aKey:1,bKey:6,cKey:5},{bKey:1,cKey:4},{bKey:6,cKey:7}]

var result = array.sort(function(hash) {
  return function(a, b) {
    return ((a.aKey || Infinity) - (b.aKey || Infinity)) 
    || ((a.bKey || Infinity) - (b.bKey || Infinity)) 
    || ((a.cKey || Infinity) - (b.cKey || Infinity))
  }
}(Object.create(null)));

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

But you want bKey:2 to come before bKey:1 as for the last element that has aKey, the value of bKey is 2.

To adjust for this anomaly, without knowing which element is to follow once aKey is done with (and extending to the case where bKey is also done with too), you can do this - hash these anomaly keys and sort accordingly - see demo below:

var array=[{aKey:2,bKey:2,cKey:3},{aKey:1,bKey:6,cKey:5},{bKey:1,cKey:4},{bKey:6,cKey:7},{bKey:2,cKey:7},{bKey:2,cKey:6},{cKey:4},{cKey:7}]

var result = array.sort(function(hash) {
  return function(a, b) {
    // find the anomaly keys
    a.aKey && !b.aKey && (hash.bkey = a.bKey);
    a.bKey && !b.bKey && (hash.ckey = a.cKey);
    // sort criteria
    return ((a.aKey || Infinity) - (b.aKey || Infinity)) 
    || (((a.bKey != hash.bkey) - (b.bKey != hash.bkey)) || ((a.bKey || Infinity) - (b.bKey || Infinity))) 
    || (((a.cKey != hash.ckey) - (b.cKey != hash.ckey)) || ((a.cKey || Infinity) - (b.cKey || Infinity)))
  }
}(Object.create(null)));

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

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

2 Comments

@AdityaVijay let me know your feedback on this, thanks!
its working, what i didn't get is how you made it to bring bKey:2 at first position, will check that and get back to you! Thanks!
2

You can use sort() like this

var data = [{
    aKey:2, 
    bKey:2, 
    cKey:3
}, { 
    bKey:2, 
    cKey:6
}, {
    aKey:1,
    bKey:6, 
    cKey:5
}, {
    cKey:41
}, {
    cKey:7
}, {
    bKey:1, 
    cKey:4
}, {
    bKey:6, 
    cKey:7
}]


data.sort(function(a, b) {
  return ((b.aKey != undefined) - (a.aKey  != undefined) || a.aKey - b.aKey) ||
  	 ((b.bKey != undefined) - (a.bKey  != undefined) || ((a.bKey != 2) - (b.bKey != 2)) || a.bKey - b.bKey) ||
         ((b.cKey != undefined) - (a.cKey  != undefined) || a.cKey - b.cKey)
})

console.log(data)

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.