3

I have an array of Strings like so:

String[] array = { "CC/2", "DDD/3", "AAAA/4", "B/1" };
Arrays.sort(array);
System.out.println(Arrays.toString(array));

When I execute this code, I get the following:

[AAAA/4, B/1, CC/2, DDD/3]

This is correct, however I want to sort it by the numbered value instead, so that I get the following result:

[B/1, CC/2, DDD/3, AAAA/4]

How do I go about doing this?

Kurtiss
  • 495
  • 2
  • 7
  • 22
  • 6
    You'll need to [implement your own Comparator](http://stackoverflow.com/questions/5245093/using-comparator-to-make-custom-sort) – BackSlash Aug 01 '14 at 13:33

5 Answers5

4

You can do it like this by the use of Comparator<String>

    /* You may found some shortcuts for this
       but following code is easy to understand 
    */
    String[] array = { "CC/2", "DDD/3", "AAAA/4", "B/1"};
    Arrays.sort(array, new Comparator<String>() {

        public int compare(String o1, String o2) {
            int i1,i2;
            /* 
               You should add some checks like 
               1] null check
               or 
               2] whether String contains / or not etc before going for further 
               code.
            */
            /* Get Numbers from String To compare */
            i1=Integer.valueOf(o1.split("/")[1]);
            i2=Integer.valueOf(o2.split("/")[1]);
            //May throw NumberFormatException so be careful with this

            if(i1>i2)
                return 1;
            else if(i1<i2)
                return -1;
            else
                return 0;
        }
    });
    System.out.println(Arrays.toString(array));//Print array
akash
  • 22,664
  • 11
  • 59
  • 87
  • Correct, but I would add a check at the beginning of the method. What will happen if one of the parameters doesn't contain the slash? – BackSlash Aug 01 '14 at 13:41
  • @BackSlash there will always be a slash in my instance so its all good ^^ – Kurtiss Aug 01 '14 at 13:45
  • @chetan there should always be something after the slash as well, however I guess it is a good idea to include some form of error handling also – Kurtiss Aug 01 '14 at 13:50
  • 1
    Thank you all for your prompt replies, and all of the provided examples work, however I'm choosing this one as the top answer as it allows ease for data manipulation (such as error handling for example, like the slashes xp) – Kurtiss Aug 01 '14 at 13:56
  • 1
    Is there any reason for that auto-boxing and if statements? We could keep i1 and i2 as Integer and do `i1.compareTo(i2);` – Syam S Aug 01 '14 at 14:04
2

You need a custom Comparator:

Comparator<String> c = new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return s1.split("/")[1].compareTo(s2.split("/")[1]);
    }
};
String[] array = { "CC/2", "DDD/3", "AAAA/4", "B/1" };
Arrays.sort(array, c);
System.out.println(Arrays.toString(array));

This prints [B/1, CC/2, DDD/3, AAAA/4]

QBrute
  • 4,405
  • 6
  • 34
  • 40
  • 2
    You will have to convert that to an integer before comparing else string "10" is less compared to string "9". ;) – Syam S Aug 01 '14 at 14:00
2

Use a custom Comparator and remove the characters before the numbers, like this:

Arrays.sort(array, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.replaceAll("[A-Z]+/", "").compareTo(o2.replaceAll("[A-Z]+/", ""));
    }
});

The .replaceAll("[A-Z]+/", "") part removes your leading CC/, AAAA/, and so on; assuming your strings always start with at least one uppercase letter followed by a slash (/).

Zoltán
  • 21,321
  • 14
  • 93
  • 134
0

Have a look at sort(T[] a, Comparator c) in Arrays class: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

A custom comparator may be what you want

Julian
  • 21
  • 4
0

You will have to make your own sorting function. If the numbers will always be at the end of the strings you could reverse the strings using

StringBuilder().reverse().toString()

then you could sort them like you did before and then reverse them back. This will give you give you the strings in order of the numbers, but if you have multiple strings with the same number they will be sorted backwards.

Buzz
  • 1,877
  • 21
  • 25