Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 ArrayList中排序名称?_Java_Sorting_Arraylist_Comparator - Fatal编程技术网

在Java ArrayList中排序名称?

在Java ArrayList中排序名称?,java,sorting,arraylist,comparator,Java,Sorting,Arraylist,Comparator,我有一个包含姓名、年龄、薪水等的数组列表 我想按名字对名单进行排序 以下是我在班上所学的内容: //Sort by Name public ArrayList<Employee> sortByName() { Collections.sort(employees); return employees; } 我已经尝试了很多方法,但似乎没有任何效果,有人知道我哪里出了问题,或者如何让它起作用吗?谢谢。使用比较器按名称对对象排序需要第二个参数 Collections.sort(

我有一个包含姓名、年龄、薪水等的数组列表

我想按名字对名单进行排序

以下是我在班上所学的内容:

//Sort by Name
public ArrayList<Employee> sortByName()
{
  Collections.sort(employees);
  return employees;
}

我已经尝试了很多方法,但似乎没有任何效果,有人知道我哪里出了问题,或者如何让它起作用吗?谢谢。

使用比较器按名称对对象排序需要第二个参数

Collections.sort(employees, new EmployeeNameComparator());
请参阅使用自定义比较器的步骤

使用该方法使用给定的比较器对集合进行排序

 Collections.sort(employees, new EmployeeNameComparator());

请参阅重载的
Collections.sort(List,Comparator)
方法。的可能重复项
Collections.sort(employees, new EmployeeNameComparator());
 Collections.sort(employees, new EmployeeNameComparator());