使用给定的比较器作为参数对对象列表进行排序的类方法(Java)

时间:2019-01-20 16:25:44

标签: java sorting

我在定义一个方法时遇到麻烦,该方法对定义了该方法的相同类的列表进行排序。

例如,使用以下属性定义类:

          public abstract class Licence {
            //other attributes
            protected List<People> myList;

            //Constructor and other methods

            //The method I want
            public List<People> getPeopleInOrder ( Comparator c)
              List<People> aux = new ArrayList<People>(this.myList); 
              Collections.sort(aux, c);
              return aux;

我也有这堂课

                class CompPeople implements Comparator<People>{
                public int compare( People e1, People e2) {
                // Declaration the criteria of comparison
                if ( c1 == 0) {
                     if (c2 == 0 ) return c3;
                   else return c2;
                }
               else return c1;
        }
    }

但是当我调用main方法时

               List<People> myNewList = Mylicence.getPeopleInOrder(new CompPeople());

编辑:在尝试对无法修改的列表进行排序之前,我对其进行了更改,现在它给了我一个空列表。

我知道我可以使用Collections的排序方法,但是我想要的是License类的方法,该方法使用给定的比较器对列表进行排序。预先感谢

2 个答案:

答案 0 :(得分:0)

您应该可以从注释中猜测,问题出在unmodifiableList()上。对于副本,只需创建一个新的List

public List<People> getPeopleInOrder ( Comparator c)
    List<People> aux = new ArrayList<>(this.myList);
    Collections.sort(aux, c);
    return aux;
}

答案 1 :(得分:0)

我认为您的比较器不正确。

让我们定义一个People类:

public class People {
    private String name;
    private String city;
}

如果您打算使用不同的方式来比较这些对象,那么最好使用自定义比较器。例如。这是两个比较器,它们按一个字段对人进行排序。我建议将不同的比较器定义为People类的一部分:

public class People {

    private String name;
    private String city;

    public static final Comparator<People> SORT_BY_NAME_ASC = (one, two) -> one.name.compareToIgnoreCase(two.name);
    public static final Comparator<People> SORT_BY_CITY_ASC = (one, two) -> one.city.compareToIgnoreCase(two.city);

}

现在您可以使用它对List<People> peoples进行排序:

people.sort(People.SORT_BY_NAME_ASC); // sort by name asc
people.sort(People.SORT_BY_CITY_ASC); // sort by city asc
people.sort(People.SORT_BY_NAME_ASC.thenComparing(People.SORT_BY_CITY_ASC));    // sort by name and city asc

您的方法getPeopleInOrder()如下所示:

public List<People> getPeopleInOrder(Comparator<People> comparator) {
    if(myList.isEmpty())
        return Collections.emptyList();

    List<People> aux = new ArrayList<>(myList);
    aux.sort(comparator);

    return aux;
}

...或像这样使用Streams

public List<People> getPeopleInOrder(Comparator<People> comparator) {
    return myList.stream().sorted(comparator).collect(Collectors.toList());
}

如果只需要一个比较器,则有两种方法。

人们实现可比性

class People implements Comparable<People> {

    private String name;
    private String city;

    @Override
    public int compareTo(People people) {
        int res = name.compareToIgnoreCase(people.name);
        res = res == 0 ? city.compareToIgnoreCase(people.city) : res;
        // compare other fields you want
        return res;
    }
}

使用单个比较器

public class People {

    private String name;
    private String city;

    public static final Comparator<People> SORTY_BY_NAME_AND_CITY = (one, two) -> {
        int res = one.name.compareToIgnoreCase(two.name);
        res = res == 0 ? one.city.compareToIgnoreCase(two.city) : res;
        // compare other fields you want
        return res;
    };
}