1

Hold Down! before marking as duplicate or voting down

let's say I have a java Script Object as below and I wanna to sort this object by values, yes I know array is invented for it and has builtin sort but for some reason I have to go with this object :(

var myObj = {"you": 100, "me": 75,"11111": 20, "foo": 116, "bar": 15};

till now I end up with

{11111: 20, bar: 15, me: 75, you: 100, foo: 116}

but you can see it's not what you want, as in that case, I tried many solution from this deprived solution given here the code is below

Object
 .keys(myObj)
 .sort((a, b) => myObj[a]-myObj[b])
 .reduce((_sortedObj, key) => ({
   ..._sortedObj, 
   [key]: myObj[key]
 }), {}) 

Edit I see this question already it's not my case as I have numeric values so not any single answer given there helpful for me

Abdul Hameed
  • 1,008
  • 1
  • 15
  • 35
  • 1
    since you have numerical keys, this is not possible – adiga Jul 25 '19 at 13:41
  • 1
    @adiga yes it's looks impossible in my mind, that's why I am asking here :( – Abdul Hameed Jul 25 '19 at 13:42
  • 1
    I might be mistaken, but I thought that the properties of an object were unordered. :-/ (see https://stackoverflow.com/questions/7214293/is-the-order-of-elements-in-a-json-list-preserved) – StardustGogeta Jul 25 '19 at 13:44
  • 1
    Possible duplicate of [Sorting JavaScript Object by property value](https://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value) – Satej S Jul 25 '19 at 13:45

2 Answers2

4

It is not possible.

In ES2015+, the traversal order of object properties are:

  • First, the keys that are integer indices, in ascending numeric order.
  • Then, all other string keys, in the order in which they were added to the object.
  • Lastly, all symbol keys, in the order in which they were added to the object.

Even if you add the 11111 key at the end, it will always be fetched first when you use Object.keys() or for...in loops

const obj = {};
obj["first"] = 1
obj["second"] = 2
obj["200"] = 200 // integer keys
obj["100"] = 100 

// 100 and 200 property will be moved to the top even if they are added in the end
// 200 was added before 100, but it will be printed after 100 
// because integer indices are traversed in ascending numeric order
console.log(obj) 

// 100 will be the first key
console.log(Object.keys(obj))

You could use use a Map object instead. It has key-value pairs and it remembers the original insertion order of the keys.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • so it's impossible? can't do with any ugliest solution as well – Abdul Hameed Jul 25 '19 at 13:47
  • 1
    @AbdulHameed no, you cannot do it. Relying on property ordering (even though it's now well-defined behavior) is a really bad idea because it makes fairly broad assumptions about object "life history". It's a recipe for lots of terrible bugs. – Pointy Jul 25 '19 at 13:48
2

Don't know if this answer could possibly help you:

Sort a dictionary by value in JavaScript

After running the code in that answer on your dictionary I got this:

enter image description here

Is this something you could work with?

SP de Wit
  • 215
  • 1
  • 9
  • I think now it's looks impossible then I might be go with this solution, at least in that case I can display the correct result right now – Abdul Hameed Jul 25 '19 at 14:05