Sort months ( with strings ) algorithm

前端 未结 10 1158
深忆病人
深忆病人 2021-01-18 03:42

I have this months array:

[\"January\", \"March\", \"December\" , \"October\" ]

And I want to have it sorted like this:

[\"         


        
10条回答
  •  逝去的感伤
    2021-01-18 04:17

    If I had a way to supply a custom sorting order, I'd create a list defining the correct order:

    correct = List("January", "February", "March", ...)
    

    And then sort by the position in that list, something like:

    toSort.sort(a, b) => compare(correct.index(a), correct.index(b))
    

提交回复
热议问题