Javascript's sort is 'unstable' - how do I get around this?

后端 未结 1 2069
天涯浪人
天涯浪人 2020-12-19 13:27

According to the MDN spec, Javascript\'s sort() function is \'unstable\' (does not maintain input order for identical elements).

Ironically, it seems that Firefox cu

1条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 14:15

    Here is an example of a working stable sort. It adds an extra parameter for the original item index which is used if items are otherwise "equal". The code should work in any browser in use.

    Note that it is an example only and should be modified to suit your particular circumstance.

    
    
    
    0
    2
    0

    The extra parameter is added as an attribute and could be removed when elements are added back to the container to keep things clean.

    0 讨论(0)
提交回复
热议问题