Sanju Sanju - 1 year ago 70
Java Question

How do I sort strings that contain numbers in Java

I want to sort a String that has nr. How do I do that?

Lets say my integers are


in main I do class2.Sort();

Thanks in Advance.

Answer Source
  public static void main(String[] args)
    String string = "3 42 \n   11   \t  7  dsfss  365          \r   1";
    String[] numbers = string.split("\\D+");
    Arrays.sort(numbers, new Comparator<String>()
      public int compare(String s1, String s2)
        return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download