fork(1) download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) {
  5. List<String> strings = Arrays.asList("room1.2", "foo1.1", "foo", "room2.3", "room100.999", "room10", "room.3");
  6.  
  7. Collections.sort(strings, new Comparator<String>() {
  8. public int compare(String o1, String o2) {
  9. return extractInt(o1) - extractInt(o2);
  10. }
  11.  
  12. int extractInt(String s) {
  13. String num = s.replaceAll("\\D", "");
  14. // return 0 if no digits found
  15. return num.isEmpty() ? 0 : Integer.parseInt(num);
  16. }
  17. });
  18. System.out.println(strings);
  19. }
  20. }
Success #stdin #stdout 0.1s 38924KB
stdin
Standard input is empty
stdout
[foo, room.3, room10, foo1.1, room1.2, room2.3, room100.999]