简体   繁体   English

用两个不同的对象对数组进行排序

[英]Sort an array with two different objects

I need help with sorting an array that contains two different kind of classes that I have created. 我需要对一个数组进行排序,该数组包含我创建的两种不同类型的类。 My two classes are "Human" that have a name and an age, then I have a class "Physicist" that inherits from human but also have the field "start year" when they started studying. 我的两个班级是“人类”,有一个名字和年龄,然后我班级的“物理学家”继承了人类,但是当他们开始学习时也有“开始年”这一领域。 Something like this: 像这样:

public class Human implements Comparable<Human> {

   private int age;
   private String name;

   public Human(int agein, String namein) {  
      age = agein;
      name = namein;   
   }
   public int compareTo(Human other) {

      return Integer.compare(this.age, other.age);       
   }
}

public class Fysiker extends Human {

   private int year;

   public Fysiker(int startYear, int ageIn, String nameIn){

       super(ageIn, nameIn);
       }

   public int compareTo(Fysiker other) {

       if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
           return Integer.compare(this.year, other.year);
       }
       else {
           return Integer.compare(this.getAge(), other.getAge());
       }

   }
}

What I want is that when I create an array mixed with humans and physicists and sort it, I want it to be sorted by age, and if two physicists are the same age, then they should get sorted by the year they have. 我想要的是,当我创建一个由人类和物理学家混合而成的数组并对其进行排序时,我希望按年龄对它进行排序,如果两个物理学家的年龄相同,则应该按他们拥有的年份对它们进行排序。 For example like this: 例如这样:

Input: 输入:

name: Alex, age: 32, year: 2007 名字:亚历克斯(Alex),年龄:32,年:2007

name: Nils, age: 30, year: 2008 名称:Nils,年龄:30,年份:2008

name: Anders, age: 32, year: 2003 名字:Anders,年龄:32,年份:2003

name: Erik, age: 18. 姓名:埃里克(Erik),年龄:18岁。

name: Olof, age: 31. 名字:Olof,年龄:31。

Sorted array: 排序数组:

name: Erik, age: 18. 姓名:埃里克(Erik),年龄:18岁。

name: Nils, age: 30, year: 2008 名称:Nils,年龄:30,年份:2008

name: Olof, age: 31. 名字:Olof,年龄:31。

name: Anders, age: 32, year: 2003 名字:Anders,年龄:32,年份:2003

name: Alex, age: 32, year: 2007 名字:亚历克斯(Alex),年龄:32,年:2007

Are my compareTo methods wrong? 我的compareTo方法错误吗? Or why is it not working? 还是为什么不起作用? I'm not getting any errors, the array just get sorted by age and then nothing more happens. 我没有任何错误,只是按年龄对数组进行排序,然后什么也没有发生。

I'm thankful for your help! 感谢您的帮助!

This method: 该方法:

public int compareTo(Fysiker other) {

    if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
        return Integer.compare(this.year, other.year);
    }
    else {
        return Integer.compare(this.getAge(), other.getAge());
    }

}

will never be called, because you have an array of Human so the signature doesn't match (as mentioned by Arsen in the comments). 永远不会被调用,因为您有一个Human数组,因此签名不匹配(如Arsen在评论中所述)。

This should work: 这应该工作:

public int compareTo(Human other) {

   if(other instanceof Fysiker && this.getAge() == other.getAge() ) {
       return Integer.compare(this.year, ((Fysiker) other).year);
   }
   else {
       return Integer.compare(this.getAge(), other.getAge());
   }

}

Alternatively to implementing Comparable , you could also use a custom Comparator when sorting. 除了实现Comparable ,还可以在排序时使用自定义Comparator Particularly with Java 8, this is also much simpler for comparing multiple fields, by chaining Comparator.comparing and Comparator.thenComparing with custom lambda functions. 尤其是在Java 8中,通过将Comparator.comparingComparator.thenComparing与自定义的lambda函数进行链接,比较多个字段也变得更加简单。

In any way, the Comparator (or Comparable ) has to accept any kind of Human and check whether it's a Fysiker or not. 无论如何, Comparator (或Comparable )必须接受任何类型的Human并检查它是否是Fysiker

List<Human> humans = Arrays.asList(
        new Fysiker(2007, 32, "Alex"),
        new Fysiker(2008, 30, "Nils"),
        new Fysiker(2003, 32, "Anders"),
        new Human(18, "Erik"),
        new Human(31, "Olof")
);
Collections.sort(humans, Comparator
        .comparing((Human h) -> h.age)
        .thenComparing((Human h) -> h instanceof Fysiker ? ((Fysiker) h).year : 0));
humans.forEach(System.out::println);

Output: 输出:

Human Erik 18
Fysiker Nils 30 2008
Human Olof 31
Fysiker Anders 32 2003
Fysiker Alex 32 2007

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

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