Comparator won't work with Arrays.sort

十年热恋 提交于 2019-12-01 23:14:03

问题


So I'm working on a comparator problem and I can't figure out why the Array.sort in this first class is giving me the error of:

The method sort(T[], Comparator) in the type Arrays is not applicable for the arguments (ArrayList, CalorieComparator)

Restaurant Class:

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class Restaurant {
    private ArrayList<Edible> elist;

    public Restaurant() {
    }

    public void addEdibleItem(Edible item){
        elist.add(item);
    }

    public List<Edible> orderByCalories(){
        Arrays.sort(elist, new CalorieComparator());
    }

CalorieComparator class:

import java.util.Comparator;
public class CalorieComparator implements Comparator {

    public int compare(Object o1, Object o2){
        Edible thisfood = (Edible)o1;
        Edible otherfood = (Edible)o2;
        if(thisfood.getCalories() > otherfood.getCalories())
            return 1;
        else if (thisfood.getCalories() < otherfood.getCalories())
            return -1;
        else 
            return 0;
    }
}

回答1:


An ArrayList is different from a Java array; since you're using a List, Arrays.sort won't help you here.

Consider Collections.sort instead.




回答2:


Ignoring your actual problem with Arrays.sort vs. Collections.sort (that has been beautifully answered), it might be a good idea to implement a Comparator<Edible> instead of casting your Objects in the compare method:

public class CalorieComparator implements Comparator<Edible> {

  @Override
  public int compare(Edible o1, Edible o2) {        
    if (o1.getCalories() > o2.getCalories()) {
        return 1;
    } else if (o1.getCalories() < o2.getCalories()) {
        return -1;
    } else {
        return 0;
    }
  }
}



回答3:


Arrays.sort takes an array as its first argument. To sort a collection such as a list, use Collections.sort.

Collection.sort(elist, new CalorieComparator());

Also, note that your method won't compile because you aren't returning a List<Edible>.




回答4:


Arrays.sort(elist, new CalorieComparator());

You are calling a sort() of Arrays class with ArrayList, ArrayList belongs to Collection family not an Array.

your error will resolved if you use Collectinss.sort()




回答5:


As the error message suggest the problem is actually caused by the list being passed instead where an array is expected, nothing to do with the comparator.

Try the following instead

Arrays.sort(elist.toArray(), new CalorieComparator());

But with that you will not get the desired result ultimately, because it will sort the brand new array just created instead of the list itself. But that you should get the idea of what the error message was trying to say. Just make sure not to pass a list there, it's meant to sort an array and not a list.

As others have suggested please use the Collections.sort method instead to get the desired result.

Also, just a side note, you should extend the generic version Comparator<T> instead of the non-generic one.



来源:https://stackoverflow.com/questions/12889314/comparator-wont-work-with-arrays-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!