📌  相关文章
📜  按日期对ArrayList中的对象进行排序的Java程序

📅  最后修改于: 2022-05-13 01:55:39.480000             🧑  作者: Mango

按日期对ArrayList中的对象进行排序的Java程序

最重要的工具是 sort() 方法,用于 Collections 类的比较器机制,该方法按降序排序。是的,如果在一般情况下,我们想要实现目标,考虑到要排序的对象是用户定义的边界条件,然后盲目地使用 Comparator。这两种方法都在下面讨论,其中对象也是由用户定义的类型创建的。

方法:

在Java,我们有多种方法可以按日期对 ArrayList 中的对象进行排序。这可以通过使用Comparable<>接口或Collections.sort()方法来完成,为了完成这个任务,你可以使用它们中的任何一个。

  1. 使用比较器接口
  2. 使用 Collections.sort() 方法

现在让我们一一讨论它们。

方法一:使用 Comparator 接口



Java Comparator接口用于对用户定义类的对象进行排序。通过使用Comparator<>接口,您可以根据用户定义类中定义的任何数据成员对元素进行排序。 Java.util包包含此接口。我们可以通过使用 compare() 和 compareTo() 方法来完成这项任务,它们将用于比较 DateItem 类的对象。

方法:

  • 创建一个新类并将该类命名为DateItem并创建一个 String 类型的变量,然后创建一个 DateItem 类的构造函数并在此处传递该 String 类型的变量。
  • main方法中创建一个 DateItem 类型的 ArrayList。
  • 将 DateItem 的对象存储在 ArrayList 中。
  • 创建另一个名为sortItems 的类,它实现Comparator并将我们的 DateItem 类传递给一个比较器。
  • 现在在Comparator类中创建一个比较方法,该方法返回一个整数并接受两个参数 ' DateItem '对象作为compare(Object obj1, Object obj2 )。
  • 在返回值的比较方法中,使用compareTo()方法,该方法将通过比较 DateItem 对象来返回指定的值。
  • 现在在main方法中使用Collections.sort()方法并将ArrayList和 ' SortItem '对象传递给它,它将对日期进行排序,并生成输出。

示例 1

Java
// Java Program to Sort Objects in ArrayList by Date
// Using Comparator interface
 
// Importing required classes
import java.util.*;
 
// Class 1
// helper class for DateItem
class DateItem {
 
    // Member variable of this class
    String date;
 
    // Constructor of this class
    DateItem(String date)
    {
 
        // This keyword refers to current object itself
        this.date = date;
    }
}
 
// Class 2
// Helper class implementing Comparator
// from the Comparable interface
class sortItems implements Comparator {
 
    // Method of this class
    // @Override
    public int compare(DateItem a, DateItem b)
    {
 
        // Returning the value after comparing the objects
        // this will sort the data in Ascending order
        return a.date.compareTo(b.date);
    }
}
 
// Class 3
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating ArrayList class object
        // Declaring object of type-DateItem
        // class(user-defined)
        ArrayList dateList = new ArrayList<>();
 
        // Adding data to the ArrayList
        // using standard add() method
        dateList.add(new DateItem("2020-03-25"));
        dateList.add(new DateItem("2019-01-27"));
        dateList.add(new DateItem("1998-01-27"));
        dateList.add(new DateItem("1998-02-26"));
 
        // Sorting the ArrayList
        // using Collections.sort() method
        Collections.sort(dateList, new sortItems());
 
        // Display message
        System.out.println("Sorted in Ascending Order");
 
        // Iterating the list using for-each loop
        for (DateItem d : dateList) {
 
            // Printing the sorted items from the List
            System.out.println(d.date);
        }
    }
}


Java
// Java Program to Sort Objects in ArrayList by Date
// Using Comparator interface
 
// Importing required classes
import java.util.*;
 
// Class 1
// Helper class
class DateItem {
 
    // Member variable
    String date;
 
    // Constructor of this class
    DateItem(String date)
    {
 
        // this keyword refers to current instance itself
        this.date = date;
    }
}
 
// Class 2
// Helper class implementing Comparable interface
class sortItems implements Comparator {
    // @Override
 
    // Method of this class
    // To compare datetime objects
    public int compare(DateItem a, DateItem b)
    {
 
        // Returning the value after comparing the objects
        // this will sort the data in Descending order
        return b.date.compareTo(a.date);
    }
}
 
// Class 3
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an ArrayList of DateItem class
        // (user-defined)
        ArrayList dateList = new ArrayList<>();
 
        // Adding data to the ArrayList
        // using standard add() method
        dateList.add(new DateItem("2020-03-25"));
        dateList.add(new DateItem("2019-01-27"));
        dateList.add(new DateItem("1998-01-27"));
        dateList.add(new DateItem("1998-02-26"));
 
        // Sorting the elements on ArrayList object above
        Collections.sort(dateList, new sortItems());
 
        // Display message only
        System.out.println("Sorted in Descending Order");
 
        // Iterating the List
        // using for-each loop
        for (DateItem d : dateList) {
 
            // Printing the sorted items from the List
            System.out.println(d.date);
        }
    }
}


Java
// Java Program to Sort Objects in ArrayList by Date
// Using Collections.sort() method
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an ArrayList of String to
        // store the Dates
        ArrayList datesList = new ArrayList<>();
 
        // Adding date to ArrayList
        // using standard add() method
        datesList.add("2020-03-25");
        datesList.add("2019-01-27");
        datesList.add("2020-03-26");
        datesList.add("2020-02-26");
 
        // Display message only
        System.out.println(
            "Dates Object before sorting : ");
 
        // Iterating in the ArrayList
        // using for each loop
        for (String dates : datesList) {
 
            // Printing the data from the list
            System.out.println(dates);
        }
 
        // Sorting the ArrayList
        // using Collections.sort() method
        Collections.sort(datesList);
 
        // Display message only
        System.out.println("Dates Object after sorting : ");
 
        // Iterating in the ArrayList
        // using for-each loop
        for (String dates : datesList) {
 
            // Printing the data from the list
            System.out.println(dates);
        }
    }
}



输出
Sorted in Ascending Order
1998-01-27
1998-02-26
2019-01-27
2020-03-25

示例 2



Java

// Java Program to Sort Objects in ArrayList by Date
// Using Comparator interface
 
// Importing required classes
import java.util.*;
 
// Class 1
// Helper class
class DateItem {
 
    // Member variable
    String date;
 
    // Constructor of this class
    DateItem(String date)
    {
 
        // this keyword refers to current instance itself
        this.date = date;
    }
}
 
// Class 2
// Helper class implementing Comparable interface
class sortItems implements Comparator {
    // @Override
 
    // Method of this class
    // To compare datetime objects
    public int compare(DateItem a, DateItem b)
    {
 
        // Returning the value after comparing the objects
        // this will sort the data in Descending order
        return b.date.compareTo(a.date);
    }
}
 
// Class 3
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating an ArrayList of DateItem class
        // (user-defined)
        ArrayList dateList = new ArrayList<>();
 
        // Adding data to the ArrayList
        // using standard add() method
        dateList.add(new DateItem("2020-03-25"));
        dateList.add(new DateItem("2019-01-27"));
        dateList.add(new DateItem("1998-01-27"));
        dateList.add(new DateItem("1998-02-26"));
 
        // Sorting the elements on ArrayList object above
        Collections.sort(dateList, new sortItems());
 
        // Display message only
        System.out.println("Sorted in Descending Order");
 
        // Iterating the List
        // using for-each loop
        for (DateItem d : dateList) {
 
            // Printing the sorted items from the List
            System.out.println(d.date);
        }
    }
}


输出
Sorted in Descending Order
2020-03-25
2019-01-27
1998-02-26
1998-01-27

方法二:使用 Collections.sort() 方法

Collections.sort()方法可用于对自定义对象的 ArrayList 进行排序。我们可以使用此方法按日期对 ArrayList 中的对象进行排序。 Java.util.Collections.sort()方法存在于Java.util.Collections 类中。它用于按升序对指定集合列表中存在的元素进行排序。它的工作原理类似于Java.util.Arrays.sort() 方法,但它比它更好,因为它可以对 Array 的元素进行排序以及 链表、队列和更多的东西都在其中。

例子

Java

// Java Program to Sort Objects in ArrayList by Date
// Using Collections.sort() method
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an ArrayList of String to
        // store the Dates
        ArrayList datesList = new ArrayList<>();
 
        // Adding date to ArrayList
        // using standard add() method
        datesList.add("2020-03-25");
        datesList.add("2019-01-27");
        datesList.add("2020-03-26");
        datesList.add("2020-02-26");
 
        // Display message only
        System.out.println(
            "Dates Object before sorting : ");
 
        // Iterating in the ArrayList
        // using for each loop
        for (String dates : datesList) {
 
            // Printing the data from the list
            System.out.println(dates);
        }
 
        // Sorting the ArrayList
        // using Collections.sort() method
        Collections.sort(datesList);
 
        // Display message only
        System.out.println("Dates Object after sorting : ");
 
        // Iterating in the ArrayList
        // using for-each loop
        for (String dates : datesList) {
 
            // Printing the data from the list
            System.out.println(dates);
        }
    }
}


输出
Dates Object before sorting : 
2020-03-25
2019-01-27
2020-03-26
2020-02-26
Dates Object after sorting : 
2019-01-27
2020-02-26
2020-03-25
2020-03-26