How to sort Alphanumeric String

前端 未结 11 1470
我寻月下人不归
我寻月下人不归 2020-12-10 06:25

I have a problem with sorting strings which include integers. If I use the below code I get sorting like: 1some, 2some, 20some, 21some, 3some, some

However I want it

11条回答
  •  星月不相逢
    2020-12-10 07:06

    You can't use the default String compareTo() instead need compare the Strings following the below algorithm.

    1. Loop through the first and second String character by character and get a chunk of all strings or numbers
    2. Check if the chunks are numbers or strings
    3. If numbers sort numerically else use String compareTo()

    Repeat the steps.

提交回复
热议问题