Alphabetically Sort a Java Collection based upon the 'toString' value of its member items

前端 未结 10 1454
心在旅途
心在旅途 2020-12-29 22:10

Assume I have a user defined Java class called Foo such as:

public class Foo 
{

    private String aField;

    @Override
    public String toString()
    {         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 22:45

    Use the API sort(List list, Comparator c)which specifies a comparator, and implement is as you wish.

    Alternatively, if you do not specifically need a List, use a SortedSet, same goes with the comparator.

提交回复
热议问题