Sorting custom class array-list string using Collections.sort

前端 未结 2 1165
不思量自难忘°
不思量自难忘° 2021-02-06 17:47

I am trying to sort my custom class array-list using Collections.sort by declaring my own anonymous comparator. But the sort is not working as expected.

My code is

2条回答
  •  后悔当初
    2021-02-06 18:35

    As indicated by Adam, you can use return (lhs.Name.compareTo(rhs.Name)); likeso:

    Collections.sort(arrlstContacts, new Comparator() {
         public int compare(Contacts lhs, Contacts rhs) {
             return (lhs.Name.compareTo(rhs.Name));
         }
    });
    

提交回复
热议问题