1

I have Object with arrays like below , I am trying to sort it ascending and descending by its value (rank)

AXISBANK: [rank: 110, bnrank: 110, bs: 75, ss: 3]
BANKBARODA: [rank: 32, bnrank: 5, bs: 83, ss: 26]
HDFCBANK: [rank: 453, bnrank: 453, bs: 52, ss: 33]

So output array is like below if descending .. and reverse for ascending

HDFCBANK: [rank: 453, bnrank: 453, bs: 52, ss: 33]
AXISBANK: [rank: 110, bnrank: 110, bs: 75, ss: 3]
BANKBARODA: [rank: 32, bnrank: 5, bs: 83, ss: 26]

I tried following code

scorearr.sort((obj1, obj2) => obj1.bnrank - obj2.bnrank);

And also below code

function compares(a,b) {
console.log(a.bnrank);
  if (a.bnrank < b.bnrank)
    return -1;
  if (a.bnrank > b.bnrank)
    return 1;
  return 0;
}
scorearr.sort(compares);

Still scorearr is unchanged and its in old order only.

Edit : Below is sample data

{
  "HDFCBANK": {
    "rank": 453,
    "bnrank": 453,
    "bs": 52,
    "ss": 33
  },
  "ICICIBANK": {
    "rank": 228,
    "bnrank": 228,
    "bs": 88,
    "ss": 3
  },
  "KOTAKBANK": {
    "rank": 164,
    "bnrank": 164,
    "bs": 23,
    "ss": 82
  }
}

This is how I am creating this object ,please tell me if any alternative if this thing can be done easily.

var scorearr = {};
//loop here
var id = $(this).attr('id');
scorearr[id] = {};
scorearr[id] = {rank:nfscore,bnrank:bnscore,bs:sellscore,ss:buyscore};
//end loop
2
  • 3
    Data shown doesn't have valid structure. Please provide a proper example as per minimal reproducible example Commented Dec 24, 2018 at 15:48
  • A JavaScript object is an unordered collection - meaning the order of it's properties aren't guaranteed. Consider using an array (or Map if you're using ES6). See this SO post: stackoverflow.com/questions/5525795/… Commented Dec 24, 2018 at 16:48

1 Answer 1

3

If you have an object with nested objects, you could take the entries (key/value pairs), sort this array and rebuild a new object with the sorted order.

var data = { AXISBANK: { rank: 110, bnrank: 110, bs: 75, ss: 3 }, BANKBARODA: { rank: 32, bnrank: 5, bs: 83, ss: 26 }, HDFCBANK: { rank: 453, bnrank: 453, bs: 52, ss: 33 } },
    sorted = Object.assign(...Object
        .entries(data)
        .sort(({ 1: { bnrank: a } }, { 1: { bnrank: b } }) => b - a)
        .map(([k, v]) => ({ [k]: v }))
    );

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

Another approach with an array.

var data = [{ id: 'AXISBANK', rank: 110, bnrank: 110, bs: 75, ss: 3 }, { id: 'BANKBARODA', rank: 32, bnrank: 5, bs: 83, ss: 26 }, { id: 'HDFCBANK', rank: 453, bnrank: 453, bs: 52, ss: 33 }];


data.sort(({ bnrank: a }, { bnrank: b }) => b - a);

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

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

8 Comments

Hi , I have arrays inside object , you have given example of objects in object i gues..
please add that array, you have. the above data in the post is not valid and not an array either.
Hi , I have added that array in my question, it looks similiar to you . but in developer console its showing object in old order only.
which variable do you have in the console.log? maybe an array would work better ...
I did console.log(sorted); , other questions says that object sorting is not guaranteed ,, please see my question , I have edited how i create this object, can you tell me some other method to create this object and make task easier.
|

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.