使用日期对数组进行排序(快速排序)

Sorting an array with dates (Quicksort)

我的目标是根据事件的日期对矩阵进行排序(在矩阵中存储为事件 [eventIndex][1]。不知何故我得到了几乎正确的输出,除了 中显示的部分粗体

我必须分别对年、月、日进行排序吗? 还是我的比较方法有逻辑错误?

排序前:
2015 年 12 月 24 日
2015 年 12 月 19 日
2015 年 12 月 30 日
2015 年 11 月 13 日
2015 年 12 月 30 日
2016 年 1 月 15 日
2015 年 12 月 31 日
2016 年 1 月 15 日
2015 年 12 月 24 日
2015 年 12 月 19 日
2015 年 12 月 31 日
2016 年 1 月 15 日

排序后:
2015 年 11 月 13 日
2015 年 12 月 19 日
2015 年 12 月 19 日
2015 年 12 月 24 日
2015 年 12 月 24 日
2015 年 12 月 30 日
2015 年 12 月 31 日
2015 年 12 月 30 日
2015 年 12 月 31 日

2016 年 1 月 15 日
2016 年 1 月 15 日
2016 年 1 月 15 日

这是我的代码。

public void quickSort(String[][] event, int low, int high, Compare c) {
    if (event == null || event.length == 0)
        return;

    if (low >= high)
        return;
    // pick the pivot
    int middle = low + (high - low) / 2;

    // make left < pivot and right > pivot
    int i = low, j = high;
    while (i <= j) {
        while (c.compare(i, middle)) {
            i++;
        }
        while (c.compare(middle, j)) {
            j--;
        }
        if (i <= j) {
            String[] temp = event[i];
            event[i] = event[j];
            event[j] = temp;
            i++;
            j--;
        }
    }
    // recursively sort two sub parts
    if (low < j)
        quickSort(event, low, j,c);

    if (high > i)
        quickSort(event, i, high,c);
}

//Interface for comparing two types
public interface Compare {
    boolean compare(int first, int second);
}
public class CompareDate implements Compare {

    @Override
    public boolean compare(int first, int second) {
        //Splitting up the date string and converts into int
        //Splitting first index
        String[] temp = event[first][1].split("/");
        int firstYear = Integer.parseInt(temp[2]);
        int firstMonth = Integer.parseInt(temp[0]);
        int firstDay = Integer.parseInt(temp[1]);

        //Splitting second index
        temp = event[second][1].split("/");
        int secondYear = Integer.parseInt(temp[2]);
        int secondMonth = Integer.parseInt(temp[0]);
        int secondDay = Integer.parseInt(temp[1]);

        //Comparing the values
        if (firstYear < secondYear) return true;
        else if (secondYear < firstYear) return false;
        else if (firstMonth < secondMonth) return true;
        else if (secondMonth < firstMonth) return false;
        return (firstDay < secondDay);
    }
}

我能想到的更简单的方法是将日期转换为长整数,然后进行比较。更简单。

另一种选择是使用 Java 日历 class。

https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

这是一个使用递归的很好的解决方案。它可能不是最佳的,但它工作正常。

public static void quickSort(String[] event) {
    String temp;
    int a, b, c, d, e, f;

    // Sorting years
    for (int i = 0 ; i < event.length - 1 ; i++){
        a = Integer.valueOf(event[i].split("/")[2]);
        b = Integer.valueOf(event[i+1].split("/")[2]);
        // Sorting years
        if (a > b){
            temp = event[i];
            event[i] = event[i+1];
            event[i+1] = temp;
            quickSort(event);
        } else if (a == b){
            c = Integer.valueOf(event[i].split("/")[0]);
            d = Integer.valueOf(event[i+1].split("/")[0]);
            // Sorting months
            if (c > d){
                temp = event[i];
                event[i] = event[i+1];
                event[i+1] = temp;
                quickSort(event);
            } else if (c == d){
                e = Integer.valueOf(event[i].split("/")[1]);
                f = Integer.valueOf(event[i+1].split("/")[1]);
                // Sorting days
                if (e > f){
                    temp = event[i];
                    event[i] = event[i+1];
                    event[i+1] = temp;
                    quickSort(event);
                }
            }
        }
    }
}