0

I have a String[][] array with following data. I want to sort this array

  1. according to the integer value in column data[][1],
  2. according to the date in column data[][2]. Output should be 2D String array.
String[][] data = {
    {"text", "5", "Fri, 04 Feb 2011 13:17:31 GMT"},
    {"text", "5", "Fri, 04 Feb 2011 14:24:47 GMT"},
    {"text", "8", "Fri, 05 Sep 2014 20:43:57 GMT"},
    {"text", "4", "Fri, 08 Nov 2013 16:35:34 GMT"},
    {"text", "4", "Fri, 15 Aug 2014 10:00:16 GMT"},
    {"text", "4", "Fri, 16 May 2014 15:03:08 GMT"},
    {"text", "6", "Fri, 31 Jan 2014 13:12:06 GMT"},
    {"text", "4", "Mon, 04 Aug 2014 09:14:36 GMT"},
    {"text", "4", "Mon, 08 Sep 2014 03:55:54 GMT"},
    {"text", "1", "Mon, 08 Sep 2014 12:53:09 GMT"}
};
2
  • what is the desired output? is it again a 2D-String[][] ? Sep 11, 2014 at 10:55
  • Yes. 2D String. Thx.
    – arun
    Sep 11, 2014 at 10:56

2 Answers 2

1

I hope sorting the integer won't be a issue to you i will show you how you can sort according to date

Arrays.sort(data, new Comparator<String[]>() { // sort the array
    SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    // SimpleDateFormat  to parse the String Date
    @Override
    public int compare(String[] o1, String[] o2) {
        try {
            return f.parse(o1[2]).compareTo(f.parse(o2[2]));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
});
for(int i = 0; i < data.length; i++){ // display the result
    for(int j = 0; j < data[i].length; j++)
    {
        System.out.print(data[i][j]);
        if(j < data[i].length - 1) System.out.print(" ");
    }
    System.out.println();
}
0
1

Just Wrap the Array into a list using Arrays.asList.

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList%28T...%29

Now use Collections.sort and create a custom Comparator that reads the data from the arrays inside the list and sorts accordingly.

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29

First compare on the integer, if the integers are the same then parse the string into a Date and compare the two Date objects.

4
  • your post is not very newbie-friendly but ist totally correct!! Sep 11, 2014 at 11:02
  • 1
    @MartinFrank It has all the information he needs, I'm not going to write his code for him :) If he has specific questions he wants to add when trying to follow this I'll be happy to answer them though.
    – Tim B
    Sep 11, 2014 at 11:03
  • 100% agreed! i didn't mean to criticise you instead i apprechiate (damn english, sorry) your approach! Sep 11, 2014 at 11:05
  • Also possible to sort using Arrays.sort(Object[], Comparator).
    – clstrfsck
    Sep 11, 2014 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.