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
Gracie williams
  • 1,287
  • 2
  • 16
  • 39
  • 3
    Data shown doesn't have valid structure. Please provide a proper example as per [mcve] – charlietfl Dec 24 '18 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: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Tom O. Dec 24 '18 at 16:48

1 Answers1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hi , I have arrays inside object , you have given example of objects in object i gues.. – Gracie williams Dec 24 '18 at 16:04
  • 1
    please add that array, you have. the above data in the post is not valid and not an array either. – Nina Scholz Dec 24 '18 at 16:06
  • 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. – Gracie williams Dec 24 '18 at 16:48
  • which variable do you have in the console.log? maybe an array would work better ... – Nina Scholz Dec 24 '18 at 16:54
  • 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. – Gracie williams Dec 24 '18 at 16:57
  • with actual javascript, object have a defined order. it is values, who could be indices of an array are sorted first, then all other keys in insertation order. the above approach creates a new object with properties in a new order. the result is an order object. if you need to have an object with the bank name as accessor, then the object is fine. if you need to take all banks in a distinct order, then an array would be better with an additional key for the ... key. either way, it's up to the use for the data structure. – Nina Scholz Dec 24 '18 at 17:02
  • So what can i do , even if i paste your code in chrome console , its sorting by bank name by default. – Gracie williams Dec 24 '18 at 17:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185737/discussion-between-nina-scholz-and-gracie-williams). – Nina Scholz Dec 24 '18 at 17:16