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.

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you sort the student list in Java by ascending order of student names. Here's an example code snippet that demonstrates how to achieve this using the `Collections.sort()` method: ``` import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public static void main(String[] args) { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("Alice", 21)); studentList.add(new Student("Bob", 19)); studentList.add(new Student("Charlie", 20)); // Sort the student list by ascending order of student names Collections.sort(studentList, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { return s1.getName().compareTo(s2.getName()); } }); // Print the sorted student list for (Student student : studentList) { System.out.println(student.getName() + " - " + student.getAge()); } } } ``` In this example, we create a `Student` class that has `name` and `age` properties. We then create a `List` of `Student` objects and add some students to it. To sort the list by ascending order of student names, we use the `Collections.sort()` method and pass in a custom comparator that compares students by their names using the `compareTo()` method. Finally, we iterate over the sorted list and print out the names and ages of the students.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值