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

前端 未结 10 1392
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2020-12-29 22:42

    Sort ArrayList on POJO int property (Ascending or Descending):

    Collections.sort(myPojoList, (a, b) -> a.getId() - b.getId()); //ASC
    Collections.sort(myPojoList, (a, b) -> b.getId() - a.getId()); //DESC
    
    //Alternatively use Integer.compare()
    Collections.sort(myPojoList, (a, b) -> Integer.compare(a.getId(), b.getId())); //ASC
    Collections.sort(myPojoList, (a, b) -> Integer.compare(b.getId(), a.getId())); //DESC
    

    Sort ArrayList on POJO String property:

    Collections.sort(myPojoList, (a, b) -> a.getName().compareTo(b.getName())); //ASC
    Collections.sort(myPojoList, (a, b) -> b.getName().compareTo(a.getName())); //DESC
    

    Sort ArrayList on POJO Boolean property:

    Collections.sort(myPojoList, (a, b) -> Boolean.compare(a.isMale(), b.isMale())); //ASC
    Collections.sort(myPojoList, (a, b) -> Boolean.compare(b.isMale(), a.isMale())); //DESC
    

    Extra: get distinct ArrayList

    myPojoList= new ArrayList<>(new LinkedHashSet<>(myPojoList)); //Distinct
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 22:46
    Collections.sort(fooList,
                     new Comparator<Foo>()
                     {
                         public int compare(Foo f1, Foo f2)
                         {
                             return f1.toString().compareTo(f2.toString());
                         }        
                     });
    

    Assuming that toString never returns null and that there are no null items in the list.

    0 讨论(0)
  • 2020-12-29 22:48
    public class Foo
       implements Comparable<Foo>
    {
    
        private String aField;
    
        public Foo(String s)
           {
           aField=s;
            }
    
    
        public String getAField()
            {
            return aField;
            }
    
       public int compareTo(Foo other)
            {
            return getAField().compareTo(other.getAField());
            }
    
    
        @Override
        public String toString()
        {
        return getAField();
        }
    
    }
    

    and then

    Collections.sort(list);

    0 讨论(0)
  • 2020-12-29 22:50

    google-collections makes this really easy with Ordering:

    Collections.sort(list, Ordering.usingToString());
    

    Is bringing in a whole 3rd-party library just to use something you could write trivially using a Comparator (as others have provided) worthwhile? No, but google-collections is so cool you'll want to have it anyway for a bunch of other reasons.

    On the sorting front, you can also easily do things like reversing:

    Ordering.usingToString().reverse();
    

    or break ties:

    Ordering.usingToString().compound(someOtherComparator);
    

    or deal with nulls:

    Ordering.usingToString().nullsFirst();
    

    etc., but there's a bunch more stuff in there (not just sorting-related, of course) that leads to really expressive code. Check it out!

    0 讨论(0)
提交回复
热议问题