0

I try to sort object by value. The object contains some keys as a numbers which can't be sorted out.

The object looks like:

{"0":1,"1":10,"2":8,"3":5,"4":2,"5":1,"6":4,"7":1,"8":3,"9":4,"-":107,"a":7,"b":14,"c":12,"d":15,"e":3,"f":35,"g":5,"h":5,"i":8,"j":1,"k":14,"l":7,"m":18,"n":3,"o":6,"p":20,"r":5,"s":36,"t":17,"u":8,"v":5,"w":4,"y":1,"q":1,"x":1,"а":2,"б":1,"в":3,"г":2,"д":7,"е":1,"з":2,"и":3,"к":4,"л":3,"м":5,"н":3,"о":1,"п":7,"р":1,"с":4,"т":1,"у":1,"ч":4,"ш":1,"э":6}

The code is as follows:

let group_sorted = {}

const sorted = Object.keys(group)
             .sort((a,b) => { return group[b] - group[a]} )

console.log(sorted) // <= returns sorted keys just fine


for (let elm of sorted) {

    elm = elm.toString() // <= convert numbers to string // didn't help
    group_sorted[elm] = group[elm]
}

console.log(group_sorted)

The result I get looks like the following (everything except numbers sorted out properly)

{"0":1,"1":10,"2":8,"3":5,"4":2,"5":1,"6":4,"7":1,"8":3,"9":4,
"-":107,"s":36,"f":35,"p":20,"m":18,"t":17,"d":15,"b":14,"k":14,"c":12,"i":8,"u":8,"a":7,"l":7,"д":7,"п":7,"o":6,"э":6,"g":5,"h":5,"r":5,"v":5,"м":5,"w":4,"к":4,"с":4,"ч":4,"e":3,"n":3,"в":3,"и":3,"л":3,"н":3,"а":2,"г":2,"з":2,"j":1,"y":1,"q":1,"x":1,"б":1,"е":1,"о":1,"р":1,"т":1,"у":1,"ш":1}
Systems Rebooter
  • 822
  • 10
  • 23
  • @Camilo Unfortunately no. I have some numbers as a keys and unlike letters numbers not sorted out properly – Systems Rebooter Apr 28 '20 at 17:43
  • 2
    Actually it does answer your question. From there: "*JavaScript objects are not ordered. It is meaningless to try to "sort" them*". So don't try to build an object. Whatever you need done in order, base if off your `sorted` *array* of keys. – Bergi Apr 28 '20 at 17:50

2 Answers2

0

There is a specific order that javascript objects end up in:

https://2ality.com/2015/10/property-traversal-order-es6.html

The reason why the numbers are out of order is because the spec says that number indices are iterated first before string indices.

Zachary Haber
  • 7,380
  • 1
  • 6
  • 19
0

group={"0":1,"1":10,"2":8,"3":5,"4":2,"5":1,"6":4,"7":1,"8":3,"9":4,
"-":107,"s":36,"f":35,"p":20,"m":18,"t":17,"d":15,"b":14,"k":14,"c":12,"i":8,"u":8,"a":7,"l":7,"д":7,"п":7,"o":6,"э":6,"g":5,"h":5,"r":5,"v":5,"м":5,"w":4,"к":4,"с":4,"ч":4,"e":3,"n":3,"в":3,"и":3,"л":3,"н":3,"а":2,"г":2,"з":2,"j":1,"y":1,"q":1,"x":1,"б":1,"е":1,"о":1,"р":1,"т":1,"у":1,"ш":1}

result=Object.entries(group).sort((a,b) => { return Object.values(a)[1] - Object.values(b)[1]} )
console.log(result);
Object.entries({"0":1,"7":10,"2":8}).sort((a,b) => { return Object.values(a)[1] - Object.values(b)[1]} )

Output:

0: (2) ["0", 1]
1: (2) ["5", 1]
2: (2) ["7", 1]
3: (2) ["j", 1]
4: (2) ["y", 1]
5: (2) ["q", 1]
6: (2) ["x", 1]
7: (2) ["б", 1]
8: (2) ["е", 1]
9: (2) ["о", 1]
10: (2) ["р", 1]
11: (2) ["т", 1]
12: (2) ["у", 1]
13: (2) ["ш", 1]
14: (2) ["4", 2]
15: (2) ["а", 2]
16: (2) ["г", 2]
17: (2) ["з", 2]
18: (2) ["8", 3]
19: (2) ["e", 3]
20: (2) ["n", 3]
21: (2) ["в", 3]
22: (2) ["и", 3]
23: (2) ["л", 3]
24: (2) ["н", 3]
25: (2) ["6", 4]
26: (2) ["9", 4]
27: (2) ["w", 4]
28: (2) ["к", 4]
29: (2) ["с", 4]
30: (2) ["ч", 4]
31: (2) ["3", 5]
32: (2) ["g", 5]
33: (2) ["h", 5]
34: (2) ["r", 5]
35: (2) ["v", 5]
36: (2) ["м", 5]
37: (2) ["o", 6]
38: (2) ["э", 6]
39: (2) ["a", 7]
40: (2) ["l", 7]
41: (2) ["д", 7]
42: (2) ["п", 7]
43: (2) ["2", 8]
44: (2) ["i", 8]
45: (2) ["u", 8]
46: (2) ["1", 10]
47: (2) ["c", 12]
48: (2) ["b", 14]
49: (2) ["k", 14]
50: (2) ["d", 15]
51: (2) ["t", 17]
52: (2) ["m", 18]
53: (2) ["p", 20]
54: (2) ["f", 35]
55: (2) ["s", 36]
56: (2) ["-", 107]
upog
  • 4,309
  • 6
  • 30
  • 59