如何在Android RecyclerView上对字符串排序? [英] How to sort Strings on an Android RecyclerView?
问题描述
我有一个充满CardViews的recyclerView,其中有2个参数(它们都是字符串),其中一个是标题,我想有一个按钮,可以根据标题按字母顺序对它们进行排序.
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 based on the tittle.
因为它没有太多元素,所以我决定使用o(n ^ 2)的插入排序,这是我的实现:
since it doesn't have too many elements I decided to use the insertion sort which is o(n^2) and this is my implementation:
public void ISortDes(String[]strings){
int j;
String key;
int i;
for (j = 1; j < strings.length; j++)
{
key = strings[ j ];
for(i = j - 1; (i >= 0) ; i--)
{
if (key.compareTo(strings[i]) > 0){
break;
}
strings[ i+1 ] = strings[ i ];
}
strings[ i+1 ] = key;
}
for (int k = 0; k < strings.length; k++){
System.out.println(strings[k]);
}
}
它需要一个字符串数组并对其进行排序.
It takes an array of Strings and orders them.
这是我的RecyclerView的采用参数的方法:
And this is the method of my RecyclerView that takes the parameters:
private void initializeData() {
categories = new ArrayList<>();
categories.add(new Categories("CARS", "CARS"));
categories.add(new Categories("SPORTS", "SPORTS"));
categories.add(new Categories("GAMING", "GAMING"));
categories.add(new Categories("GAMBLING", "GAMBLING"));
categories.add(new Categories("TECH", "TECH"));
categories.add(new Categories("NATURE", "NATURE"));
categories.add(new Categories("RANDOM", "RANDOM"));
categories.add(new Categories("COUSINE", "COUSINE"));
categories.add(new Categories("HISTORY", "HISTORY"));
categories.add(new Categories("MUSIC", "MUSIC"));
categories.add(new Categories("STUDIES", "STUDIES"));
}
我认为我需要以某种方式将第一个参数传递给字符串数组,然后对其进行排序.
I think that i need to pass somehow that first paramater to a String Array and then sort it.
想法是在显示RecyclerView的同一Activity中的按钮中具有排序方法,并且在按下该按钮时必须对它们进行排序,而无需转到另一个Activity.
The idea is to have the sorting method in a button in the same Activity where the RecyclerView is displayed and when pressed it would have to sort them without going to another Activity.
我有点迷路了.
在简历中,我要做的是创建一个按字母顺序根据标题"参数对RecyclerView的元素(在本例中为CardView)进行排序的按钮.
In resume what i'm trying to do is to have a buttom that orders the elements (which in this case are CardViews) of a RecyclerView alphabetically based on the "title" paramater.
我的想法正确吗,您还有其他方法可以做到这一点吗?或者我应该怎么做才能做到这一点?
Is my idea right, do you have another way to do this, or what should i do in order to accomplish this?
感谢一百万.
推荐答案
对列表进行排序的最简单方法是使用java.util.Collections
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);
}
});
这将逐个字符比较标题.并将您的列表从a到z排序.
This will compare the title character by character. and will sort your list from a to z.
别忘了在修改后通知列表,数据已经用notifyDataSetChanged()
(来自您的RecyclerView.Adapter
)更改了.
Don't forget after the modification to notify the list that the data have changed with notifyDataSetChanged()
(from your RecyclerView.Adapter
).
这篇关于如何在Android RecyclerView上对字符串排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!