I have a string arraylist names
which contains names of people. I want to sort the arraylist in alphabetical order.
我有一个字符串arraylist名称,其中包含人的名字。我想按字母顺序对arraylist进行排序。
ArrayList<String> names = new ArrayList<String>();
names.add("seetha");
names.add("sudhin");
names.add("Swetha");
names.add("Neethu");
names.add("ananya");
names.add("Athira");
names.add("bala");
names.add("Tony");
names.add("Karthika");
names.add("Nithin");
names.add("Vinod");
names.add("jeena");
Collections.sort(names);
for(int i=0; i<names.size(); i++)
System.out.println(names.get(i));
I tried to sort the list in above way. But it is displaying the sorted array as:
我试图以上述方式对列表进行排序。但是它将排序的数组显示为:
Athira
Karthika
..
..
ananya
bala
...
but I don't want to make it case sensitive. I want the result as:
但我不想让它区分大小写。我希望结果如下:
ananya
Athira
bala
6 个解决方案
#1
281
Custom Comparator
should help
自定义比较器应该有所帮助
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
Or if you are using Java 8:
或者如果您使用的是Java 8:
list.sort(String::compareToIgnoreCase);
#2
174
The simplest thing to do is:
最简单的事情是:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
#3
28
try this code
试试这段代码
Collections.sort(yourarraylist, new SortBasedOnName());
import java.util.Comparator;
import com.RealHelp.objects.FBFriends_Obj;
import com.RealHelp.ui.importFBContacts;
public class SortBasedOnName implements Comparator
{
public int compare(Object o1, Object o2)
{
FBFriends_Obj dd1 = (FBFriends_Obj)o1;// where FBFriends_Obj is your object class
FBFriends_Obj dd2 = (FBFriends_Obj)o2;
return dd1.uname.compareToIgnoreCase(dd2.uname);//where uname is field name
}
}
#4
5
Based on the above mentioned answers, I managed to compare my custom Class Objects like this:
根据上面提到的答案,我设法比较我的自定义类对象,如下所示:
ArrayList<Item> itemList = new ArrayList<>();
...
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item, Item t1) {
String s1 = item.getTitle();
String s2 = t1.getTitle();
return s1.compareToIgnoreCase(s2);
}
});
#5
3
You need to use custom comparator which will use compareToIgnoreCase
, not compareTo.
您需要使用自定义比较器,它将使用compareToIgnoreCase,而不是compareTo。
#6
2
Starting from Java 8 you can use Stream
:
从Java 8开始,您可以使用Stream:
List<String> sorted = Arrays.asList(
names.stream().sorted(
(s1, s2) -> s1.compareToIgnoreCase(s2)
).toArray(String[]::new)
);
It gets a stream from that ArrayList
, then it sorts it (ignoring the case). After that, the stream is converted to an array which is converted to an ArrayList
.
它从ArrayList获取一个流,然后对其进行排序(忽略大小写)。之后,流被转换为一个转换为ArrayList的数组。
If you print the result using:
如果使用以下方法打印结果:
System.out.println(sorted);
you get the following output:
你得到以下输出:
[ananya, Athira, bala, jeena, Karthika, Neethu, Nithin, seetha, sudhin, Swetha, Tony, Vinod]
#1
281
Custom Comparator
should help
自定义比较器应该有所帮助
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
Or if you are using Java 8:
或者如果您使用的是Java 8:
list.sort(String::compareToIgnoreCase);
#2
174
The simplest thing to do is:
最简单的事情是:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
#3
28
try this code
试试这段代码
Collections.sort(yourarraylist, new SortBasedOnName());
import java.util.Comparator;
import com.RealHelp.objects.FBFriends_Obj;
import com.RealHelp.ui.importFBContacts;
public class SortBasedOnName implements Comparator
{
public int compare(Object o1, Object o2)
{
FBFriends_Obj dd1 = (FBFriends_Obj)o1;// where FBFriends_Obj is your object class
FBFriends_Obj dd2 = (FBFriends_Obj)o2;
return dd1.uname.compareToIgnoreCase(dd2.uname);//where uname is field name
}
}
#4
5
Based on the above mentioned answers, I managed to compare my custom Class Objects like this:
根据上面提到的答案,我设法比较我的自定义类对象,如下所示:
ArrayList<Item> itemList = new ArrayList<>();
...
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item, Item t1) {
String s1 = item.getTitle();
String s2 = t1.getTitle();
return s1.compareToIgnoreCase(s2);
}
});
#5
3
You need to use custom comparator which will use compareToIgnoreCase
, not compareTo.
您需要使用自定义比较器,它将使用compareToIgnoreCase,而不是compareTo。
#6
2
Starting from Java 8 you can use Stream
:
从Java 8开始,您可以使用Stream:
List<String> sorted = Arrays.asList(
names.stream().sorted(
(s1, s2) -> s1.compareToIgnoreCase(s2)
).toArray(String[]::new)
);
It gets a stream from that ArrayList
, then it sorts it (ignoring the case). After that, the stream is converted to an array which is converted to an ArrayList
.
它从ArrayList获取一个流,然后对其进行排序(忽略大小写)。之后,流被转换为一个转换为ArrayList的数组。
If you print the result using:
如果使用以下方法打印结果:
System.out.println(sorted);
you get the following output:
你得到以下输出:
[ananya, Athira, bala, jeena, Karthika, Neethu, Nithin, seetha, sudhin, Swetha, Tony, Vinod]