Sorting alphanumeric strings java

后端 未结 4 733
孤城傲影
孤城傲影 2020-12-01 20:28

I have this array storing the suffix of some URLs the user is adding:

[U2, U3, U1, U5, U8, U4, U7, U6]

When I do this:

for          


        
4条回答
  •  半阙折子戏
    2020-12-01 20:42

    Just use Collections.sort() method after creating an ArrayList of your values like this:

    ArrayList a = new ArrayList();
    a.add("U2");
    a.add("U1");
    a.add("U5");
    a.add("U4");
    a.add("U3");
    System.out.println("Before : "+a);
    Collections.sort(a);
    System.out.println("After : "+a);
    

    Output :

    Before : [U2, U1, U5, U4, U3]
    After : [U1, U2, U3, U4, U5]
    

提交回复
热议问题