📌  相关文章
📜  使用自定义比较器按升序对日期数组进行排序

📅  最后修改于: 2021-05-17 20:48:46             🧑  作者: Mango

给定N个日期的数组arr [] ,格式为“ DD-MM-YYYY”,任务是按升序对这些日期进行排序。

例子:

方法:

  • 创建一个自定义比较器函数,该函数比较两个日期,如下所示:
    • 首先比较两个元素的年份。具有较大年份的元素将排在其他元素之后。
    • 如果两个日期的年份相同,则比较月份。具有较大月份的元素将排在另一个元素之后。
    • 如果两个日期的月份相同,则比较日期。日期较大的元素将排在其他元素之后。
  • 然后使用定义的自定义比较器对数组进行排序。在C++中,它是通过以下方式完成的:
  • 打印修改后的数组。

下面是上述方法的实现:

C++
// C++ implementation to sort the
// array of dates in the form of
// "DD-MM-YYYY" using custom comparator
 
#include 
using namespace std;
 
// Comparator to sort the array of dates
int myCompare(string date1,
              string date2)
{
    string day1 = date1.substr(0, 2);
    string month1 = date1.substr(3, 2);
    string year1 = date1.substr(6, 4);
 
    string day2 = date2.substr(0, 2);
    string month2 = date2.substr(3, 2);
    string year2 = date2.substr(6, 4);
 
    // Condition to check the year
    if (year1 < year2)
        return 1;
    if (year1 > year2)
        return 0;
 
    // Condition to check the month
    if (month1 < month2)
        return 1;
    if (month1 > month2)
        return 0;
 
    // Condition to check the day
    if (day1 < day2)
        return 1;
    if (day1 > day2)
        return 0;
}
 
// Function that prints the
// dates in ascensding order
void printDatesAscending(
    vector arr)
{
    // Sort the dates using library
    // sort function with custom Comparator
    sort(arr.begin(), arr.end(), myCompare);
 
    // Loop to print the dates
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << "\n";
}
 
// Driver Code
int main()
{
    vector arr;
    arr.push_back("25-08-1996");
    arr.push_back("03-08-1970");
    arr.push_back("09-04-1994");
    arr.push_back("29-08-1996");
    arr.push_back("14-02-1972");
 
    printDatesAscending(arr);
 
    return 0;
}


Java
// Java implementation to sort the
// array of dates in the form of
// "DD-MM-YYYY" using custom comparator
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function that prints the
// dates in ascensding order
static void printDatesAscending(ArrayList arr)
{
     
    // Sort the dates using library
    // sort function with custom Comparator
    Collections.sort(arr,new Comparator<>()
    {
        public int compare(String date1, String date2)
        {
            String day1 = date1.substring(0, 2);
            String month1 = date1.substring(3, 5);
            String year1 = date1.substring(6);
           
            String day2 = date2.substring(0, 2);
            String month2 = date2.substring(3, 5);
            String year2 = date2.substring(6);
             
            // Condition to check the year
            if (year2.compareTo(year1) > 0)
                return -1;
            else if (year2.compareTo(year1) < 0)
                return 1;
             
            // Condition to check the month    
            else if (month2.compareTo(month1) > 0)
                return -1;
            else if (month2.compareTo(month1) < 0)
                return 1;
             
            // Condition to check the day
            else if (day2.compareTo(day1) > 0)
                return -1;
            else
                return 1;
        }
    });
     
    // Loop to print the dates
    for(int i = 0; i < arr.size(); i++)
        System.out.println(arr.get(i));
} 
 
// Driver code
public static void main(String[] args)
{
    ArrayList arr = new ArrayList<>();
    arr.add("25-08-1996");
    arr.add("03-08-1970");
    arr.add("09-04-1994");
    arr.add("29-08-1996");
    arr.add("14-02-1972");
     
    printDatesAscending(arr);
}
}
 
// This code is contributed by offbeat


输出:
03-08-1970
14-02-1972
09-04-1994
25-08-1996
29-08-1996

时间复杂度: O(N * log N)