How to sort Strings on an Android RecyclerView?

前端 未结 1 1271
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 03:11

I have a recyclerView full of CardViews that have 2 paramteres (Both of them are Strings), one of those is a title,I would like to have a button to sort them alphabetically base

相关标签:
1条回答
  • 2021-02-08 04:04

    The easiest way to sort a list is to use java.util.Collections

    Collections.sort(categories, new Comparator<Categories>() {
                @Override
                public int compare(Categories lhs, Categories rhs) {
                    return lhs.title.compareTo(rhs.title);
                }
            });
    

    This will compare the title character by character. and will sort your list from a to z.

    Don't forget after the modification to notify the list that the data have changed with notifyDataSetChanged() (from your RecyclerView.Adapter).

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