简体   繁体   English

基于Java中属性数据的对象的订单清单

[英]Order list with objects based on attributes data in Java

I need to order my List with 'Foo' objects based on a Enum value that Foo has as an attribute. 我需要基于Foo具有作为属性的Enum值来对List和'Foo'对象进行排序。 I found 'Comparators', and I have used them before. 我找到了“比较器”,并且以前使用过它们。 But I always had some integer / double attribute, which is more clearer for me. 但是我总是有一些integer / double属性,对我来说更清楚。

Because this question might be vague, i'll make some spoonfed code so you guys can see what I mean / want. 因为这个问题可能含糊不清,所以我将编写一些代码,以便大家了解我的意思/想要的内容。 Thanks a bunch! 谢谢一群!

/* Enum class */
public enum SomeEnum {
   CONSTANT_ALPHA, CONSTANT_BETA, CONSTANT_DELTA;
}

/* Class 'Foo', this is my Foo object */
public class Foo {
    private SomeEnum enum; /* Every 'Foo' has a enum type */

    public Foo(SomeEnum enum){ 
        this.enum = enum; 
    }

    public SomeEnum getEnum(){ 
        return enun;
    }
}

/* Imagine this below is some controller class*/

 Foo alpha1 = new Foo(SomeEnum.CONSTANT_ALPHA);
 Foo beta1 = new Foo(SomeEnum.CONSTANT_BETA);
 Foo alpha2 = new Foo(SomeEnum.CONSTANT_ALPHA);
 Foo delta1 = new Foo(SomeEnum.CONSTANT_DELTA);
 Foo beta2 = new Foo(SomeEnum.CONSTANT_BETA);

 List<Foo> list = new ArrayList<>();
 list.add(alpha1);
 //list.add(beta1) etc etc (all those 5 Foos)

 Collections.sort(list, someComparatorIDontKnowHowToMake);

 System.out.println(list.toString()); /* Not sure how to correctly print a List<> object, but imagine I would print the contents of the list*/

 /* The result of the list should be: */
 /* alpha1, alpha2, beta1, beta2, delta1*/

In conclusion or TL;DR: 结论或TL; DR:

I need to order the objects in the list based with the same Enum types. 我需要使用相同的Enum类型对列表中的对象进行排序。 I do not need to have 'alpha' before 'delta' specifically. 我不需要在“ delta”之前先加上“ alpha”。 I just want that the Foo objects that have the same enum value are next to eachother in the List. 我只希望具有相同枚举值的Foo对象在列表中彼此相邻。

If this didn't make sense, please do ask! 如果这没有道理,请提出要求!

Thanks. 谢谢。

You can simply compare enum elements of Foo 您可以简单地比较Foo enum元素

foos.sort(new Comparator<Foo>() {

    @Override
    public int compare(Foo o1, Foo o2) {
        //Add null check
        return  o1.getEnum().compareTo(o2.getEnum());
    }
});

Can't test the code right know, so there might be some syntax issues, but to the point: enum instances are distinct integers, so you can compare Foo instances just by them. 无法正确地测试代码,因此可能存在一些语法问题,但要点是:枚举实例是不同的整数,因此您可以仅通过它们比较Foo实例。

Collections.sort(list, new Comparator<Foo>(){
        public compare(Foo first, Foo second){
            return first.getEnum().comapareTo(second.getEnum());
        }
    });

for(Foo item : list){
    System.out.println(item);
}

Note that this code assumes that getEnum() won't return null for any instance in the list and that you will have toString() method implemented for Foo. 请注意,此代码假定getEnum()不会为列表中的任何实例返回null,并且您将为Foo实现toString()方法。

edit , yep, always too late. 编辑 ,是的,总是为时已晚。

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

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