You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于ID与时间的ArrayList双规则排序实现问询

Hey there! Let's tackle these two sorting requirements step by step. First, here's your sample list for reference:

ArrayList<MyObject> list = new ArrayList<MyObject>();
list.add(new MyObject (1, "2011-04-27T09:40:01.607"));
list.add(new MyObject (1, "2011-05-27T09:42:01.607"));
list.add(new MyObject (2, "2011-06-27T09:42:01.607"));
list.add(new MyObject (5, "2011-07-27T09:43:01.607"));
list.add(new MyObject (6, "2011-08-27T09:44:01.607"));
list.add(new MyObject (6, "2011-09-27T09:45:01.607"));
list.add(new MyObject (1, "2011-10-27T09:46:01.607"));

First, make sure your MyObject class has getter methods for id and timestamp (we need these to access fields for sorting). Here's a basic implementation:

class MyObject {
    private int id;
    private String timestamp;

    // Constructor
    public MyObject(int id, String timestamp) {
        this.id = id;
        this.timestamp = timestamp;
    }

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

    public String getTimestamp() {
        return timestamp;
    }

    // Optional: Override toString for easy output checking
    @Override
    public String toString() {
        return "(" + id + ", \"" + timestamp + "\")";
    }
}

需求1:按ID排序

To sort the list by ID in ascending order, you have two straightforward options:

Option 1: Use a custom Comparator (no changes to MyObject)

This is flexible if you don't want to alter the original class:

import java.util.Collections;
import java.util.Comparator;

Collections.sort(list, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        // Compare IDs directly using Integer's built-in comparison
        return Integer.compare(o1.getId(), o2.getId());
    }
});

Option 2: Implement Comparable in MyObject (natural ordering)

If ID should be the default sort order for MyObject, modify the class:

class MyObject implements Comparable<MyObject> {
    // ... existing fields, constructor, getters ...

    @Override
    public int compareTo(MyObject other) {
        return Integer.compare(this.id, other.id);
    }
}

Then just call the simple sort method:

Collections.sort(list);

Either approach will give you the expected sorted list grouped by ascending ID, preserving the original timestamp order for entries with the same ID.

需求2:ID分组后按时间降序排序

Here, we first sort by ID (ascending), then reverse the timestamp order within each ID group. Since your timestamps use ISO 8601 format, we can compare the strings directly (they’re lexicographically ordered correctly).

Java 7 and below: Traditional Comparator

Collections.sort(list, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        // First compare IDs to group entries
        int idComparison = Integer.compare(o1.getId(), o2.getId());
        if (idComparison != 0) {
            return idComparison;
        }
        // For same IDs, reverse the timestamp comparison to get descending order
        return o2.getTimestamp().compareTo(o1.getTimestamp());
    }
});

Java 8+: Concise Lambda Syntax

If you’re on Java 8 or later, use lambda expressions for cleaner code:

list.sort(Comparator.comparing(MyObject::getId)
                    .thenComparing(MyObject::getTimestamp, Comparator.reverseOrder()));

Note: Your expected output has a typo where some timestamps share the same minute value, but the code above will correctly sort based on the actual timestamp strings from your sample data.

内容的提问来源于stack exchange,提问作者ahmad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:39:16