How to prevent automatic sort of Object numeric property?

后端 未结 10 1270
醉酒成梦
醉酒成梦 2020-12-03 20:57

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

10条回答
  •  心在旅途
    2020-12-03 21:46

    I've stumbled with this issue with our normalised array which keyed with Ids> After did my research, I found out there's no way to fix using the object keys because by default the Javascript is sorting any object key with number when you iterate it.

    The solution I've done and it worked for me is to put a 'sortIndex' field and used that to sort the list.

提交回复
热议问题