0

When I try to add an Object it just automatically sorts it, which I don't want

console.log(hwidList.csgo)

hwidList.csgo[hwidList.csgo.length] = { "HWID": 3267523467, "Name": "NewUser2", "ID": 34645674573464, "Date": "04/01/2021:22:47:59" };
console.log(hwidList.csgo)

It sorts it alphabetical

It sorts it alphabetical

Help please

Edit: Thanks for the help! It's the browser / console.log which shows it sorted but it isn't.

Image of it in terminal and it's not sorted

RequestFX
  • 17
  • 5
  • 5
    If order matters, then an object is the wrong data structure. Consider using an Array or [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). – Nick Jan 04 '21 at 22:00
  • Objects don't really have sorting order in JS. There is consistent implementation of this amongst the JS engines but it's not defined in the standards so don't rely on it. – Dominik Jan 04 '21 at 22:01
  • so you need to figure correct index of your array if order matters, if not just use push method – Nonik Jan 04 '21 at 22:02
  • @Dominik [The order is defined.](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – iota Jan 04 '21 at 22:02
  • @iota please link to the spec for it. I can't find the order spec in your linked question – Dominik Jan 04 '21 at 22:05
  • 1
    @Dominik [relevant question for object property order](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties) it has most spec links, if not, you can find it in the spec pretty fast. – ASDFGerte Jan 04 '21 at 22:07
  • @Dominik You can look for it on ecma-international.org. Here is one relevant method: http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys – iota Jan 04 '21 at 22:10
  • This is great, thank you @iota – Dominik Jan 04 '21 at 22:10
  • @Dominik No problem. – iota Jan 04 '21 at 22:11

1 Answers1

1

What browser are you using? For example, in Firefox, when calling console.log the result is printed alphabetically for your convenience. This issue has nothing to do with JavaScript or your code, it is the browser trying to help you in the instance you have very large objects.

  • First of all thank you for your quick response didn't expect that. Yes I tested it with firefox. I am not quite convinced that its the browsers fault. I tried it with chrome and microsoft edge aswell but no luck [In Chrome][1] [In Edge][2] [1]: https://i.stack.imgur.com/lbC1E.png [2]: https://i.stack.imgur.com/ohPjs.png – RequestFX Jan 04 '21 at 22:11
  • I can confidently assure you this is the browser trying to help you. Additionally, as other comments have stated if order matters you are using a fundamentally incorrect data structure. If you found my answer helpful and answers your question, can you please accept? – Harley Thomas Jan 04 '21 at 23:53