How to sort a List<Object> alphabetically using Object name field

后端 未结 16 1492
暗喜
暗喜 2020-11-28 06:00

I have a List of Objects like List p.I want to sort this list alphabetically using Object name field. Object contains 10 field and name field is o
16条回答
  •  盖世英雄少女心
    2020-11-28 06:26

    Have a look at Collections.sort() and the Comparator interface.

    String comparison can be done with object1.getName().compareTo(object2.getName()) or object2.getName().compareTo(object1.getName()) (depending on the sort direction you desire).

    If you want the sort to be case agnostic, do object1.getName().toUpperCase().compareTo(object2.getName().toUpperCase()).

提交回复
热议问题