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.
我有一个回收视图,里面有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:
因为它没有太多的元素,我决定使用插入排序是o(n ^ 2),这是我的实现:
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.
它接受一个字符串数组并对它们进行排序。
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.
我认为我需要以某种方式将第一个参数传递给一个字符串数组,然后对它进行排序。
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.
我们的想法是在一个按钮中有一个排序方法,在同一个活动中显示回收视图,当它被按下时,它必须对它们进行排序,而不需要进入另一个活动。
I'm kinda lost here.
我有点失落。
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.
在简历中,我要做的是有一个按钮,按字母顺序,按字母顺序排列元素(在本例中是CardViews)。
Is my idea right, do you have another way to do this, or what should i do in order to accomplish this?
我的想法是对的,你有别的办法吗?或者我该怎么做才能实现?
Thanks a million.
由于一百万年。
1 个解决方案
#1
22
The easiest way to sort a list is to use java.util.Collections
排序列表的最简单方法是使用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.
这将按字符对标题字符进行比较。将你的列表从a到z排序。
Don't forget after the modification to notify the list that the data have changed with notifyDataSetChanged()
(from your RecyclerView.Adapter
).
在修改之后,不要忘记通知列表,该数据已经通过notifyDataSetChanged()(来自于您的recycle . adapter)进行了更改。
#1
22
The easiest way to sort a list is to use java.util.Collections
排序列表的最简单方法是使用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.
这将按字符对标题字符进行比较。将你的列表从a到z排序。
Don't forget after the modification to notify the list that the data have changed with notifyDataSetChanged()
(from your RecyclerView.Adapter
).
在修改之后,不要忘记通知列表,该数据已经通过notifyDataSetChanged()(来自于您的recycle . adapter)进行了更改。