1

I have a 2d array of integers which hold a date: the month is at index 0, the day at index 1, and the year at index 2

date [0][0] = 10;
date [0][1] = 14;
date [0][2] = 2013;
System.out.println(date[0][0] + "-" + date[0][1] + "-" + date[0][2]);

OUTPUT:

10-14-2013

If I had lots of these, would it be possible to sort these dates in chronological order?

This is what I have so far:

for(int i =0; i < date.length; i++)
{
    Arrays.sort(date[i]);
}

but this, instead, switches my day with the month.

After searching, I found that a few people on the stack were using the comparator class for sorting. Is it possible to use that?

DannyD
  • 2,732
  • 16
  • 51
  • 73

5 Answers5

1

why don't you just use an array of Date objects? It seems to implement what you are trying to do with this 2D array already.

Actually should use Calendar.set which replaces the link above

SeekingAlpha
  • 7,489
  • 12
  • 35
  • 44
1

Although it would make more sense to use a date object (you're in object denial), it isn't hard to sort this 2D array using a custom comparator:

Arrays.sort(data, new Comparator<int[]>() {
    public int compare(int[] a, int[] b)  {
        // compare years
        if (a[2] == b[2]) {
            // compare months
            if (a[0] == b[0]) {
                return Integer.compare(a[1], b[1]);
            } else {
                // compare days
                return Integer.compare(a[0], b[0]);
            }
        } else {
            return Integer.compare(a[2], b[2]);
        }
    }
});
Community
  • 1
  • 1
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
0

Abstract date attributes day, month, year into a custom Date object.

public class Date{
    int day;
    int month;
    int year;

} 

Your Date class should implement comparator/comparable interface and then provide compare/comareTo method implementations.

Then you are ready to sort your array/list of Date objects using inbuilt Java's sort method.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
0

Make a Date Object. Just parse your string into month,day and year. Then implement the compareTo interface.

Public class Date implements compareTo{
private int day,month,year;

public Date(int d,int m,int y ){

day=d;
month=m;
year=y;

}
public int compareTo(Date date){
if(year<date.year)
return -1;
else if(year>date.year)
return 1; 
else if(year==date.year)
if(month<date.month)
return -1
else if (month>date.month)
return 1; 
else if(month==date.month)
if (day>date.day)
return 1;
else if (day<date.day)
return -1;
else return 0; //i.e the two dates are equal since they have the same day,month and year

}


}

Once you do that,implement your sort method according to the CompareTo method. EDIT- And once you do that, you need a normal array not a 2D one.

Roudy Tarabay
  • 449
  • 1
  • 4
  • 18
0

If you want to keep the array structure, use some thing similar to counting/radix sort will save the day

First: for year, store all the indexes of items have the same years into one list.

For each year list, sort them by month in similar way as for year.

Do something similar for day.

Print all the index, starting from year to year. Enjoy :)

But Date type with in-built comparable method will be the best.

Note: actually, the key point is working with index and using some stable sort to sort them from 3 different perspective : year, month and day

Looking at your comment, you could use SimpleDateFormat to convert from string to date.

Pham Trung
  • 11,204
  • 2
  • 24
  • 43