我有一个 ArrayList
,其中包含两个字符串和一个 Integer
,但我只需要按字符串 Name
和字符串 LastName
,我尝试使用像这样的方法Collections.sort
Collections.sort(MyArrayList, (Contact v1, Contact v2) -> v1.getName().compareTo(v2.getname()));
但是这仅按姓名对我的 ArrayList
进行排序,并且不包括姓氏,我如何将姓氏添加到排序中?
最佳答案
使用 Comparator.comparing(Contact::getName).thenComparing(Contact::getLastName)
作为您的比较器:
Collections.sort(MyArrayList, Comparator.comparing(Contact::getName).thenComparing(Contact::getLastName));
关于java - 如何对对象的 ArrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32859309/