0

In my android app I'm trying to get a sorted array. I've got an array of arrays with a name string and a date string. I'm trying to sort the names based on the dates. something like this:

private void sortList() {
    String[][] namesAndDatesList = new String[][] {
                new String[] {"name1", "05/03/2016 02:11:33")}
            ,   new String[] {"name2", "04/03/2016 02:11:34"}
            ,   new String[] {"name3", "04/03/2016 02:11:33")}
            ,   new String[] {"name4", "02/03/2016 02:11:33")}
            ,   new String[] {"name5", "05/03/2016 02:11:38")}
    };

    String[] namesList = Collections.sort(namesAndDatesList, new StringDateComparator());

}

class StringDateComparator implements Comparator<String>
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
    public int compare(params)
    {
        try {
            // return something
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}

I would like it to output the names sorted by date and time like:

namesList = {name4, name3, name2, name1, name5}

I'm not sure how to do this.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Mischa
  • 2,069
  • 4
  • 23
  • 32
  • for starters, you are trying to do 2 things. Sort the array, and extract one information from the sub array. – njzk2 Jul 05 '16 at 18:20
  • And thats not possible? in one comperator? I can ofcourse always extract the names AFTER it's been sorted – Mischa Jul 05 '16 at 18:23
  • no, because that's not what a comparator does. You do need to first sort, then extract the values you need – njzk2 Jul 06 '16 at 01:42

3 Answers3

2

You should parse the string into a date and then compare it, for android you can use Arrays.sort method and pass an anonymous comparator

Example:

private void sort2dArray() {
String[][] namesAndDatesList = new String[][] { new String[] { "name1", "05/03/2016 02:11:33" },
    new String[] { "name2", "04/03/2016 02:11:34" }, new String[] { "name3", "04/03/2016 02:11:33" },
    new String[] { "name4", "02/03/2016 02:11:33" }, new String[] { "name5", "05/03/2016 02:11:38" } };

Arrays.sort(namesAndDatesList, new Comparator<String[]>() {

    @Override
    public int compare(String[] o1, String[] o2) {

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date d1 = null;
    Date d2 = null;
    try {
        d1 = sdf.parse(o1[1]);
        d2 = sdf.parse(o2[1]);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return d1.compareTo(d2);
    }
});
System.out.println(Arrays.deepToString(namesAndDatesList));
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • I've tried to implement this but the problem is the o1 and o2 are defined as the first item in the array so it is trying to compare the names and not the dates. What do i need to change to fix that? – Mischa Jul 05 '16 at 19:26
  • Nop... o1[1] and o2[1] are the element 2 (at index 1) in the strings.... – ΦXocę 웃 Пepeúpa ツ Jul 05 '16 at 19:37
  • Had some problems implementing the code cause of issues I didn't post, but I managed to fix em with your code. Thanks! marked your answer as correct – Mischa Jul 06 '16 at 10:23
1

This seems to be what you're looking for: java Arrays.sort 2d array

Alternatively, you could create a custom class with two properties (name, date), then create a CustomClassComparator which implements Comparator.

Community
  • 1
  • 1
bradkratky
  • 1,577
  • 1
  • 14
  • 28
0

java.time

The accepted answer is correct but uses outdated classes as doe the Question. The java.util.Date class is supplanted by the java.time framework built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The code using java.util.Date class implies that the data inputs are in UTC time zone. But we cannot make that assumption from the given data alone. So more appropriate is the LocalDateTime class which purposely lacks any offset-from-UTC or time zone info.

String input = "04/03/2016 02:11:34";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy HH:mm:ss" );
LocalDateTime ldt = LocalDateTime.parse( input , formatter );

The rest of the logic in that Answer remains the same. The LocalDateTime class implements compareTo.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154