How to sort ArrayList while implementing Parcelable

后端 未结 2 996
轮回少年
轮回少年 2021-01-28 05:59

I am trying to sort the category arraylist with Collections.sort method but have no luck with it.

Here is my code:

public class Categories i         


        
相关标签:
2条回答
  • 2021-01-28 06:08
    Collections.sort(yourListHere,new Comparator<Categories>() {
                @Override
                public int compare(Categories lhs, Categories rhs) {
                    //your sort logic here
                    return 0;
                }
    });
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-28 06:12

    You can also use custom comparator:

    public class CategoriesComparator implements Comparator<Category> {
        @Override
        public int compare(Category category1, Category category2) {
            return category1.getSomeProperty().compareTo(category2.getSomeProperty());
        }
    }
    

    When you want to compare call this:

    Collections.sort(yourListCategories, new CategoriesComparator());
    

    Hope it helps!

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