Java: sort a String array, whose strings represent int

后端 未结 8 1167
北恋
北恋 2020-12-16 14:09

I have String[] array like

{\"3\",\"2\",\"4\",\"10\",\"11\",\"6\",\"5\",\"8\",\"9\",\"7\"}

I want to sort it in numerical ord

8条回答
  •  情歌与酒
    2020-12-16 14:44

    I found this article about sorting strings by numeric sorting also for strings that may or may not contain numbers:

    The Alphanum Algorithm

    There is a Java implementation example linked from the article. With that class you should be able to sort your arrays numerically like this:

    Arrays.sort(myarray, new AlphanumComparator());
    

提交回复
热议问题