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

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

最重要的工具是sort()方法,用于集合类的比较器机制,它以递减的顺序进行排序。是的,如果在通用的情况下,我们想实现目标,考虑到要排序的对象是用户定义的边界条件,那么就盲目地使用比较器。下面将讨论这两种方法,其中的对象也是由用户定义的类型创建的。

方法:

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

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

现在让我们逐一讨论所有这些问题。

方法 1: 使用比较器接口

Java比较器接口被用来对用户定义的类的对象进行排序。通过使用比较器<>接口,你可以在用户定义的类中定义的任何数据成员的基础上对元素进行排序。java.util包包含这个接口。我们可以通过使用方法compare()和compareTo()来完成这项任务,这些方法将用于比较我们DateItem类的对象。

步骤:

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

示例 1

// 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<DateItem> {
 
    // 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<DateItem> 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);
        }
    }
}

输出

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

注意:这段代码将以升序排序日期。如果你想改变排序的顺序,你可以参考下面的程序。

示例 2

// 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<DateItem> {
    // @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<DateItem> 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

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

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

示例

// 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<String> 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程