Sort months ( with strings ) algorithm

前端 未结 10 1154
深忆病人
深忆病人 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:20

    Create a table with name->index, then sort the array based on its value in the table.

    A couple of examples may be useful, in C# arr.sort(myCompare), in Java Collections.sort(arr, myCompare), in Python arr.sort(myCompare), in PHP usort($arr, 'myCompare'), in C++ sort(vec.begin(), vec.end(), myCompare).

提交回复
热议问题