Sort months ( with strings ) algorithm

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

    Talking from a Java POV, I'm going to pimp (as I often do) google-collections (soon to be replaced with Guava):

    Arrays.sort(myMonths, Ordering.explicit("Jan", "Feb", "Mar", ....));
    

    ... and you're done.

    Don't write it yourself if someone else has done it, probably more efficiently and with a nicer API than you probably would.

    Not helpful in the general case, but just in case any Java folk have the same problem...

提交回复
热议问题