简体   繁体   中英

Arrange list string data in ascending order

I have following data:

{
  20*20,
  20*10,
  30*20,
  30*10,
  10*14,
  10*20,
  10*15
}

I have copied this data in List to access further in app but i want to arrange this list in ascending order like below:

{
   10*14,
   10*15,
   10*20,
   20*10,
   20*20,
   30*10,
   30*20,
}

I have tried sort method some how i am not able to get the expected result.

How can i do that?

Your example data does indeed sort as text alphabetically. No need to do anything but wrap enclosing quote marks needed for string literals.

List <String > list =
    new ArrayList<>(
        List.of(
              "20*20",
              "20*10",
              "30*20",
              "30*10",
              "10*14",
              "10*20",
              "10*15"
        )
    )
;
Collections.sort( list ) ;
System.out.println( list ) ;

See this code run live at IdeOne.com .

[10*14, 10*15, 10*20, 20*10, 20*20, 30*10, 30*20]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM