If you want it quick, you could use a recursive quicksort implementation that sorts by the String
part representing years first, then months, then days. In the following example, you have to provide the positions of the year (yyyy
), the month (MM
) and the day (dd
) as well as the character that delimits them in the date format pattern:
public static void main(String[] args) {
String[] birthdays = {
"10/05/1990",
"10/05/2001",
"10/05/2006",
"10/05/1960",
"10/05/2033"
};
// print the unsorted array (build up one-line representation first)
StringBuilder unsortedBirthdaysBuilder = new StringBuilder();
for (int i = 0; i < birthdays.length; i++) {
if (i != 0) {
unsortedBirthdaysBuilder.append(", ");
}
unsortedBirthdaysBuilder.append(birthdays[i]);
}
System.out.println("unsorted:\t" + unsortedBirthdaysBuilder.toString());
// do the sorting
quickBirthdaySort(birthdays, "/", 2, 1, 0);
// print the sorted array (build up one-line representation first)
StringBuilder sortedBirthdaysBuilder = new StringBuilder();
for (int i = 0; i < birthdays.length; i++) {
if (i != 0) {
sortedBirthdaysBuilder.append(", ");
}
sortedBirthdaysBuilder.append(birthdays[i]);
}
System.out.println("sorted:\t\t" + sortedBirthdaysBuilder.toString());
}
public static void quickBirthdaySort(String[] birthdays, String delimiter,
int yearPatternPosition, int monthPatternPosition,
int dayPatternPosition) {
String temp;
int a, b, c, d, e, f;
for (int i = 0; i < birthdays.length - 1; i++) {
// sort by years first
a = Integer.valueOf(birthdays[i].split(delimiter)[yearPatternPosition]);
b = Integer.valueOf(birthdays[i + 1].split(delimiter)[yearPatternPosition]);
if (a > b) {
temp = birthdays[i];
birthdays[i] = birthdays[i + 1];
birthdays[i + 1] = temp;
quickBirthdaySort(birthdays, delimiter, yearPatternPosition,
monthPatternPosition, dayPatternPosition);
} else if (a == b) {
// then sort by months
c = Integer.valueOf(birthdays[i].split(delimiter)[monthPatternPosition]);
d = Integer.valueOf(birthdays[i + 1].split(delimiter)[monthPatternPosition]);
if (c > d) {
temp = birthdays[i];
birthdays[i] = birthdays[i + 1];
birthdays[i + 1] = temp;
quickBirthdaySort(birthdays, delimiter, yearPatternPosition,
monthPatternPosition, dayPatternPosition);
} else if (c == d) {
// finally sort by days
e = Integer.valueOf(birthdays[i].split("-")[dayPatternPosition]);
f = Integer.valueOf(birthdays[i + 1].split("-")[dayPatternPosition]);
if (e > f) {
temp = birthdays[i];
birthdays[i] = birthdays[i + 1];
birthdays[i + 1] = temp;
quickBirthdaySort(birthdays, delimiter, yearPatternPosition,
monthPatternPosition, dayPatternPosition);
}
}
}
}
}
The output of this code is the following:
unsorted: 10/05/1990, 10/05/2001, 10/05/2006, 10/05/1960, 10/05/2033
sorted: 10/05/1960, 10/05/1990, 10/05/2001, 10/05/2006, 10/05/2033
The positions of the date parts are given as array indexes, virtually as if the String
was already split by the delimiter. For this example, I have taken the positions from your pattern (dd/MM/yyyy
).
Please note that I haven't compared its runtime to any alternative, that's your part, especially because you didn't give any comparison data for the runtime.
Also note that this just sorts String
s. You have to use this for sorting your objects.