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

前端 未结 10 1470
心在旅途
心在旅途 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:41

    If you want the collection to remain sorted, rather than sorting it at specific points, you could put it in a TreeSet with a defined Comparator. Otherwise, I'd use the Collections.sort method already mentioned by Yuval.

提交回复
热议问题