Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent automatic sort of Object numeric property?

Why I met this problem: I tried to solve an algorithm problem and I need to return the number which appeared most of the times in an array. Like [5,4,3,2,1,1] should return 1. And also when two number appear same time as the maximum appearance return the one came first. Like [5,5,2,2,1] return 5 because 5 appear first. I use an object to store the appearance of each number. The key is the number itself.

So When the input is [5,5,2,2,1] my object should be Object {5: 2, 2: 2, 1: 1} but actually I got Object {1: 1, 2: 2, 5: 2} So When I use for..in to iterate the object I got 2 returned instead of 5 . So that's why I asked this question.

This problem occurs in Chrome console and I'm not sure if this is a common issue: When I run the following code

var a = {};
a[0]=1;
a[1]=2;
a[2]=3;

a is: Object {0: 1, 1: 2, 2: 3}

But when I reverse the order of assignment like:

 var a = {};
 a[2]=3;
 a[1]=2;
 a[0]=1;

a is also:Object {0: 1, 1: 2, 2: 3} The numeric property automatic sorted in ascending order. I tried prefix or postfix the numeric property like

var a = {};
a['p'+0]=1;
a['p'+1]=2;
a['p'+2]=3;
console.log(a);//Object {p0: 1, p1: 2, p2: 3}

And this keep the property order. Is this the best way to solve the problem? And is there anyway to prevent this auto sort behavior? Is this only happen in Chrome V8 JavaScript engine? Thank you in advance!

like image 588
AndyHu Avatar asked Oct 26 '15 17:10

AndyHu


People also ask

Why does JavaScript automatically sort the number keys in an object?

Because JavaScript sucks. To preserve insertion order, either prepend leading zeros to each numeric key (since parseInt() won't mind those), or use an ES6 Map instead of an object.

Does object values keep order?

YES (but not always insertion order). Most Browsers iterate object properties as: Integer keys in ascending order (and strings like "1" that parse as ints) String keys, in insertion order (ES2015 guarantees this and all browsers comply)

Does List sort automatically?

Lists (and arrays) of objects that implement Comparable interface can be sorted automatically by Collections. sort() and Arrays. sort() APIs. Objects that implement this interface will be automatically sorted when put in a sorted map (as keys) or sorted set (as elements).

How do you sort a list of objects based on an attribute of the objects in JavaScript?

In JavaScript, we use the sort() function to sort an array of objects. The sort() function is used to sort the elements of an array alphabetically and not numerically. To get the items in reverse order, we may use the reverse() method.


3 Answers

target = {}
target[' ' + key] = value // numeric key

This can prevent automatic sort of Object numeric property.

like image 69
201709071716 Avatar answered Sep 25 '22 07:09

201709071716


You really can't rely on order of an object fields in JavaScript, but I can suggest to use Map (ES6/ES2015 standard) if you need to preserve order of your key, value pair object. See the snippet below:

let myObject = new Map();
myObject.set('z', 33);
myObject.set('1', 100);
myObject.set('b', 3);

for (let [key, value] of myObject) {
  console.log(key, value);
}
// z 33
// 1 100
// b 3
like image 10
theVoogie Avatar answered Sep 26 '22 07:09

theVoogie


You are using a JS object, that by definition does not keep order. Think of it as a key => value map.

You should be using an array, that will keep whatever you insert on the index you inserted it into. Think of it as a list.

Also notice that you did not in fact "reverse the order of the assignment", because you inserted elements on the same index every time.

like image 5
Marcelo Avatar answered Sep 28 '22 07:09

Marcelo