Ordene una array de fechas en orden ascendente usando Custom Comparator

Dada una array arr[] de N fechas en forma de “DD-MM-YYYY”, la tarea es ordenar estas fechas en orden ascendente.

Ejemplos:  

Entrada: arr[] = { “25-08-1996”, “03-08-1970”, “09-04-1994” } 
Salida: 
03-08-1970 
09-04-1994 
25-08-1996

Entrada: arr[] = { “08-03-1970”, “04-09-2020”, “19-04-2019″”} 
Salida: 
08-03-1970 
19-04-2019 
04-09-2020 

Acercarse:  

  • Cree una función de comparación personalizada que compare dos fechas como se muestra a continuación: 
    • Primero compare el año de los dos elementos. El elemento con mayor año vendrá después del otro elemento.
    • Si el año de ambas fechas es el mismo, compare los meses. El elemento con un mes mayor vendrá después del otro elemento.
    • Si el mes de ambas fechas es el mismo, compare las fechas. El elemento con mayor fecha vendrá después del otro elemento.
  • Luego ordene la array usando el comparador personalizado definido. En C++, se hace como:

ordenar (posición inicial, posición final, comparador) 
 

  • Imprime la array modificada.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation to sort the
// array of dates in the form of
// "DD-MM-YYYY" using custom comparator
 
#include <bits/stdc++.h>
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<string> 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<string> 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<String> 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<String> 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

Python3

# Python3 implementation to sort the
# array of dates in the form of
# "DD-MM-YYYY" using custom comparator
from functools import cmp_to_key
 
# Comparator to sort the array of dates
def myCompare(date1, date2):
     
    day1 = date1[0 : 2]   
    month1 = date1[3 : 3 + 2]
    year1 = date1[6 : 6 + 4]
     
    day2 = date2[0 : 2]
    month2 = date2[3 : 3 + 2]
    year2 = date2[6 : 6 + 4]
     
    # Condition to check the year
    if (year1 < year2):
        return -1
    if (year1 > year2):
        return 1
         
    # Condition to check the month
    if (month1 < month2):
        return -1
    if (month1 > month2):
        return 1
     
    # Condition to check the day
    if (day1 < day2):
        return -1
    if (day1 > day2):
        return 1
 
# Function that prints the
# dates in ascensding order
def printDatesAscending(arr):
     
    # Sort the dates using library
    # sort function with custom Comparator
    arr = sorted(arr, key = cmp_to_key(
        lambda a, b: myCompare(a, b)))
     
    # Loop to print the dates
    for i in range(len(arr)):
        print(arr[i])
 
# Driver Code
arr = []
arr.append("25-08-1996")
arr.append("03-08-1970")
arr.append("09-04-1994")
arr.append("29-08-1996")
arr.append("14-02-1972")
 
printDatesAscending(arr)
 
# This code is contributed by shubhamsingh10

C#

// C# implementation to sort the
// array of dates in the form of
// "DD-MM-YYYY" using custom comparator
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Comparator to sort the array of dates
static int myCompare(string date1,
                     string date2)
{
    string day1 = date1.Substring(0, 2);
    string month1 = date1.Substring(3, 2);
    string year1 = date1.Substring(6, 4);
 
    string day2 = date2.Substring(0, 2);
    string month2 = date2.Substring(3, 2);
    string year2 = date2.Substring(6, 4);
 
    // Condition to check the year
    return string.Compare(year1, year2);
 
    // Condition to check the month
    return string.Compare(month1, month2);
 
    // Condition to check the day
    return string.Compare(day1, day2);
}
 
// Function that prints the
// dates in ascensding order
static void printDatesAscending(List<string> arr)
{
     
    // Sort the dates using library
    // sort function with custom Comparator
    arr.Sort(myCompare);
 
    // Loop to print the dates
    for(int i = 0; i < arr.Count; i++)
        Console.WriteLine(arr[i]);
}
 
// Driver Code
static public void Main()
{
    List<string> arr = new List<string>();
    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 shubhamsingh10

Javascript

<script>
// Javascript implementation of the above approach
 
// Comparator to sort the array of dates
function myCompare(date1, date2)
{
    var day1 = date1.substr(0, 2);
    var month1 = date1.substr(3, 2);
    var year1 = date1.substr(6, 4);
 
    var day2 = date2.substr(0, 2);
    var month2 = date2.substr(3, 2);
    var year2 = date2.substr(6, 4);
 
    // Condition to check the year
    if (year1 < year2)
        return -1;
    if (year1 > year2)
        return 1;
 
    // Condition to check the month
    if (month1 < month2)
        return -1;
    if (month1 > month2)
        return 1;
 
    // Condition to check the day
    if (day1 < day2)
        return -1;
    if (day1 > day2)
        return 1;
}
 
// Function that prints the
// dates in ascensding order
function printDatesAscending( arr)
{
    var n = arr.length;
    // Sort the dates using library
    // sort function with custom Comparator
    arr.sort(myCompare);
     
    // Loop to print the dates
     
    for (var i = 0; i < n; i++)
        document.write(arr[i] + "<br>");
}
 
// Driver Code
var arr = [];
arr.push("25-08-1996");
arr.push("03-08-1970");
arr.push("09-04-1994");
arr.push("29-08-1996");
arr.push("14-02-1972");
 
printDatesAscending(arr);
</script>
Producción: 

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

 

Complejidad de tiempo: O(N*logN)
Espacio auxiliar: O(1)
 

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *