To sort a list of objects by a filedTime field in ascending order in Java

To sort a list of objects by a filedTime field in ascending order in Java, you can use the Collections.sort() method along with a Comparator. Below is a step-by-step example:

Example: Sorting a List of Objects by filedTime

Let's assume you have a class Record with a filedTime field and a list of Record objects that you want to sort.

Step 1: Define the Record Class
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Record {
    private int id;
    private LocalDateTime filedTime;
    private String name;

    // Constructor
    public Record(int id, String filedTime, String name) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        this.id = id;
        this.filedTime = LocalDateTime.parse(filedTime, formatter);
        this.name = name;
    }

    // Getters
    public int getId() {
        return id;
    }

    public LocalDateTime getFiledTime() {
        return filedTime;
    }

    public String getName() {
        return name;
    }

    // toString method for easy printing
    @Override
    public String toString() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return "Record{" +
                "id=" + id +
                ", filedTime=" + filedTime.format(formatter) +
                ", name='" + name + '\'' +
                '}';
    }
}
Step 2: Create and Sort the List of Record Objects
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // Create a list of Record objects
        List<Record> records = new ArrayList<>();
        records.add(new Record(1, "2023-05-20 10:00:00", "Record 1"));
        records.add(new Record(2, "2023-04-15 09:30:00", "Record 2"));
        records.add(new Record(3, "2023-06-10 11:00:00", "Record 3"));

        // Sort the list by filedTime in ascending order
        Collections.sort(records, new Comparator<Record>() {
            @Override
            public int compare(Record r1, Record r2) {
                return r1.getFiledTime().compareTo(r2.getFiledTime());
            }
        });

        // Print the sorted list
        for (Record record : records) {
            System.out.println(record);
        }
    }
}

Explanation

  1. Record Class:

    • The Record class has fields id, filedTime, and name.
    • The filedTime field is of type LocalDateTime for accurate date-time comparison.
    • The constructor parses the filedTime string into a LocalDateTime object.
    • The toString method formats the filedTime back to a string for easy printing.
  2. Sorting the List:

    • In the Main class, a list of Record objects is created and populated.
    • The Collections.sort() method is used with a custom Comparator that compares filedTime fields.
    • The compare method of the Comparator uses LocalDateTime's compareTo method to sort the records.

This approach ensures that your Record objects are sorted by the filedTime field in ascending order.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值