0

Below is my list which will always be in the form of aXXXX -

List<String> child = Arrays.asList("a5", "a10", "a2", "a1", "a40");
System.out.println(child);

My child list will always have strings in the form of aXXXX

Now I need to sort my above lists and extract the largest string from it..

So in the above example, it will be a40. I was initially thinking to use Collections.sort(child); but it's not sorting them out in the ascending order properly.

Roman C
  • 49,761
  • 33
  • 66
  • 176

4 Answers4

0

Sorting will be possible, but you have to extract the "a" at the beginning and treat the rest as an integer number to prevent the string "2" from being bigger then the string "10". Try a Comparator doing this, or create a new list containing only extracted integers.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

You was almost there. The possible solution is to write your own comparator:

Collections.sort(child,new Comparator<String>() {
    @Override
    public int compare(String lhs, String rhs) {
        \\compare 2 values
        return result;
    }
});

http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Possible values to return:

-1 : lhs < rhs
0 : lhs == rhs
+1 : lhs > rhs
nikis
  • 11,166
  • 2
  • 35
  • 45
0

You can use a Comparator which defines the sorting strategie: See also http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {

    static final Comparator<String> aStringComparator = new Comparator<String>() {
        public int compare(String s1, String s2) {
            //assumed input are strings in the form axxxx
            int numfromS1 = Integer.valueOf(s1.subSequence(1, s1.length()).toString());
            int numfromS2 = Integer.valueOf(s2.subSequence(1, s2.length()).toString());
            return numfromS1 - numfromS2;
        }
    };

    public static void main(String[] args) {
        List<String> child = Arrays.asList("a5", "a10", "a2", "a1", "a40");
        Collections.sort(child, aStringComparator);
        System.out.println(child);
    }
}
Jan Koester
  • 1,178
  • 12
  • 26
  • Thanks Jan. One last thing, currently it is sorting out in ascending order.. How would I sort this out in descending order? –  Feb 21 '14 at 18:54
  • I think you can make this for yourself if you adapt the compare method a little bit ;) – Jan Koester Feb 21 '14 at 19:08
  • Sure. But I am facing some problem in doing that as well. I have never wrote comparator before so having some issues with that.. :( –  Feb 21 '14 at 19:53
0

Others have already shown how to do this with Java 7.

This could also be done with the following using Java 8:

    List<String> child = Arrays.asList("a5", "a10", "a2", "a1", "a40");
    String result = child.stream()
            .sorted(
                    Collections.reverseOrder(
                            Comparator.comparing(
                                    c -> Integer.parseInt(c.substring(1, c.length()))
                            )
                    )
            )
            .findFirst().get();
    System.out.println(result);

What this does is:

  1. Obtain a Stream<String> from your child.
  2. Create a reversed Comparator<String>, which compares based on the lambda c -> Integer.parseInt(c.substring(1, c.length())), which parses the text from the second character onwards as a string.
  3. Finds the first result.
  4. Gets it out of the Optional<String> that findFirst() returns, with get().

The Comparator could be written more nicely by appending .reversed() to it instead of wrapping it in Collections.reverseOrder(), but I am unsure how it works as of now.

skiwi
  • 66,971
  • 31
  • 131
  • 216