简体   繁体   English

按第一个元素对对象列表进行排序,但如果相等则按第二个元素排序

[英]Sorting a List of objects by the First element but if Equal sort by the second

I have to sort a list of objects.我必须对对象列表进行排序。 I have to sort the list by the value time, however, if the time of two or more events is equal, then I have to sort it by its type.我必须按值时间对列表进行排序,但是,如果两个或多个事件的时间相等,则必须按其类型对其进行排序。 All I am looking for is a simple solution, the one I know would not be simple and wouldn't include any already created classes for sorting.我所寻找的只是一个简单的解决方案,我知道这个解决方案并不简单,并且不会包含任何已经创建的用于排序的类。

The expected list order预期的列表顺序

#ID , time, type
5 1.1 Arrived
5 1.1 Scheduled
4 1.2 Arrived
3 4.1 Arrived
2 5.4 Arrived
1 6.0 Arrived
5 61.1 Terminated
4 61.1 Scheduled
4 121.1 Terminated

The list I have:我的清单:

#ID , time, type
5 1.1 scheduled
5 1.1 arrived
4 1.2 arrived
3 4.1 arrived
2 5.4 arrived
1 6.0 arrived
4 61.1 scheduled
5 61.1 terminated
4 121.1 terminated
3 121.1 scheduled

Have a look at the code to get the things better:查看代码以使事情变得更好:

private List<Event> createEventList(List<Process> processList) {
        List<Event> list = new ArrayList<Event>();
        Event event;
        for(Process pr : processList) {
            // arrived
            int id = pr.getID();
            double time = pr.getArrivalTime();
            eventtype type = eventtype.arrived;
            event = new Event(id, time, type);
            list.add(event);

            //terminated
            time = pr.getFinishTime();
            type = eventtype.terminated;
            event = new Event(id,time,type);
            list.add(event);

            //scheduled
            time = pr.getFinishTime()-pr.getBurstTime();
            type = eventtype.scheduled;
            event = new Event(id,time,type);
            list.add(event);

        }
        list = list.stream().sorted(Comparator.comparing(Event::getTime))
                .collect(Collectors.toList());

        return list;

    }

EDIT: Event class and enum编辑:事件 class 和枚举

public class Event {

    private int pID = 0;
    private double time = 0.0;
    private eventtype type = null;
    public static enum eventtype{
        arrived,
        scheduled,
        terminated,
    }

    public Event(int pID, double time, eventtype type) {
        super();
        this.pID = pID;
        this.time = time;
        this.type = type;
    }

    @Override
    public String toString() {
        return pID + " " + Math.round(time*10)/10.0 + " " + type.toString();
    }

You can use Comparator#thenComparing to compare by the second object after comparing the first.在比较第一个之后,您可以使用 Comparator#thenComparing 通过第二个 object 进行比较。

list = list.stream().sorted(Comparator.comparing(Event::getTime).thenCompare(Event::getType))
                .collect(Collectors.toList())

You can use thenCompare() to compare second field and no need to use stream() and collect() again since you are assigning result in same list您可以使用thenCompare()比较第二个字段,无需再次使用stream()collect() ,因为您将结果分配在同一个列表中

list.sort(Comparator.comparing(Event::getTime).thenCompare(l ->l.getType().ordinal()));

Since you order by type which is an enum change the order in enum declaration for sort by .ordinal() otherwise you need to define comparator by own.由于您按枚举类型排序,因此更改枚举声明中的顺序以按.ordinal()排序,否则您需要自己定义比较器。

public static enum eventtype{
    terminated,
    arrived,
    scheduled,
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM