Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:按值排列对象列表_Java_Sorting_Object - Fatal编程技术网

Java:按值排列对象列表

Java:按值排列对象列表,java,sorting,object,Java,Sorting,Object,大家好,我有一个存储多个整数值的对象。我想它排序它多次降序,一次按“赢”值降序,另一次按“玩”值降序等。。。我如何才能做到这一点?以下是解决此问题的示例代码 public class MyObject { private int wins; private int played; // to do add getter and setter method for the above fields } L

大家好,我有一个存储多个整数值的对象。我想它排序它多次降序,一次按“赢”值降序,另一次按“玩”值降序等。。。我如何才能做到这一点?

以下是解决此问题的示例代码

    public class MyObject  {
        private int wins;
        private int played;         
        // to do add getter and setter method for the above fields
    }

    List<MyObject> objects = new ArrayList<MyObject>();
    // to do java code for creating Myobjects

首先,你所说的“我有一个存储…”的对象是什么意思?此外,您应该提供您使用的Java版本,并提供一些代码片段。如果副本没有帮助,请检查这些,以后,请先搜索,并通过您的问题展示您的努力成果,包括您的代码尝试。这将为我们带来更好的问题和更好的帮助。如果您想要一个组合顺序(即按赢的顺序和按玩的顺序相等赢的顺序),我觉得更合适。
// wins sorting
objects.sort(new Comparator<MyObject>() {
    @Override
    public int compare(MyObject m1, MyObject m2) {
        return m2.getWins() - m1.getWins();
     }
});

// Played sorting
objects.sort(new Comparator<MyObject>() {
    @Override
    public int compare(MyObject m1, MyObject m2) {
        return m2.getPlayed() - m1.getPlayed();
     }
});
objects.sort(Comparator.comparingInt(MyObject::wins) .reversed());
objects.sort(Comparator.comparingInt(MyObject::played) .reversed());