views:

188

answers:

5

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

Lets say my integers are

Class2
"3"
"4"
"1"

in main I do class2.Sort();

Thanks in Advance.

A: 

If the numbers are all single digit, split the string into a char array and sort the array. otherwise there has to be a delimiter to seperate the numbers. call string.split using that delimiter and sort the resulting array. The function to sort is Arrays.sort() if memory serves me right

javadoc references

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split%28java.lang.String%29 http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#sort%28double[]%29

Midhat
+6  A: 
  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));
      }
    });
    System.out.println(Arrays.toString(numbers));
  }
eljenso
Great solution, though it will break if there is a non-number in there:)
extraneon
I don't know if it makes any practical difference, but I think it's recommended to use Integer.valueOf(s) rather than new Integer(s). Supposedly the valueOf() call does some internal caching of instances.
gregcase
@gregcase Good suggestion, I edited the answer.
eljenso
+1  A: 

Your question is rather ill-formed, but here are a few things that you should know:

  • There is an Arrays.sort(Object[]) that you can use to sort arbitrary objects.
  • The natural ordering for String is lexicographic.
  • Java arrays are covariant: a String[] is an Object[].

So, given String[] sarr, if you want to sort it lexicographically (i.e. "1" < "10" < "2"), simply Arrays.sort(sarr); works. It doesn't matter if the strings contain numbers or not.

If you want to sort the strings as if they are numbers (i.e. "1" < "2" < "10"), then you need to convert the strings to numeric values. Depending on the range of these numbers, Integer.parseInt might do; you can always use BigInteger otherwise.

Let's assume that BigInteger is required.

You now have two options:

  • Convert String[] to BigInteger[], then since BigInteger implements Comparable<BigInteger>, you can use Arrays.sort using its natural ordering. You may then convert the sorted BigInteger[] back to String[].

  • Convert String to BigInteger "just-in-time" for comparison by a custom Comparator<String>. Since Arrays.sort uses the comparison based mergesort, you can expect O(N log N) comparisons, and therefore as many conversions.

polygenelubricants
+2  A: 

A general purpose solution is to use what's called a 'natural order comparator'.

Here's an example:

http://pierre-luc.paour.9online.fr/NaturalOrderComparator.java

Natural ordering is actually quite important in cases where a string might contain runs of numbers and you want things to sort alphabetically on the letters but numerically on the numbers. Modern versions of Windows Explorer uses this for ordering file names, for example. It's also very handy for picking out the latest version of a library based on version strings (i.e. "1.2.3" compared to to "1.20.1").

If you strings really just contain numbers (like you put in your description), then you are best off not using strings at all - create and work with Integer objects instead.

Kevin Day
This is particularly suitable for sorting data for human consumption (especially if case differences are treated as second-order differences), as it produces an order that tends to match how non-technical users imagine ordering to work.
Donal Fellows
A: 

The way to sort "things" according to some order is to create a Comparator which knows which of any two things are first according to the order, or to let the "thing" itself implement the Comparable interface so you do not need the Comparator.

If your job is to sort as integers, then consider converting to integers and then sort as the Integer class already implements Comparable.

Thorbjørn Ravn Andersen