Open rahil471 opened 6 years ago
Thanks for the detailed example. I think the 3rd line is also wrong, but the last line looks correct because the numbers will be sorted as strings.
Would you be able to do a PR with a fix and/or add these examples as tests? If not, I'll try to look into it soon.
@rahil471 I was trying some things out and I'm not able to get the keys to return properly sorted. This must be an internal JavaScript issue since JavaScript doesn't guarantee object keys will be in the same order that you add them to an object. I even went back to node 0.12.8
to see if there was an older version where this worked.
What I think is odd is that it only fails on floating point numbers (or strings that look like floating point numbers).
I'll leave this opened and update the documentation with information on this case.
I don't think it's just floating point numbers. I think it's a natural sorting issue.
That's what I was thinking at first, but at this line in the code the keys are sorted correctly. It's when adding them to the object (or when doing console.log
on the newly returned object) is when they aren't sorted anymore.
Exactly that's what I noticed when I was debugging... When the object is reconstructed it rearranges itself in it's natural order. So changing the return line to a-b
in sort-asc
and b-a
in sort-desc
won't work either.
I was aware that JavaScript does not guarantee order of the keys, but here somehow we are able to achieve that for object keys as strings or integers.
That's what I was thinking at first, but at this line in the code the keys are sorted correctly. It's when adding them to the object (or when doing console.log on the newly returned object) is when they aren't sorted anymore.
That means it's not a natural sorting issue?
At least not in this library.
This outputs the below results..