java 如何按字母顺序对字符串的 ArrayList 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34116046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I sort an ArrayList of strings by alphabetical order?
提问by Neo
I'm trying to create a program that can sort the contents of an ArrayList alphabetically. Right now I have three classes in my program...
我正在尝试创建一个程序,可以按字母顺序对 ArrayList 的内容进行排序。现在我的程序中有三个类...
Dog
狗
public class Dog {
private String name;
public Dog(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
DogList (which contains the ArrayList of type Dog)
DogList(包含 Dog 类型的 ArrayList)
import java.util.ArrayList;
public class DogList {
private ArrayList<Dog> dogList;
public DogList(){
DogList = new ArrayList<>();
}
public void setSize(int DogSize){
for(int x = 0; x <= DogSize; x++){
DogList.add(new Dog());
}
}
public ArrayList<Dog> getList(){
return dogList;
}
}
and the last class, DogSorter, which is trying to access DogList ArrayList and then trying to sort the contents of that ArrayList in alphabetical order.
最后一个类是 DogSorter,它尝试访问 DogList ArrayList,然后尝试按字母顺序对该 ArrayList 的内容进行排序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DogSorter {
private DogList list = new DogList();
public void sortDogs{
Collections.sort(list);
}
for(i = 0; i < list.length(); i++) {
System.out.println(list.getList().get(i).getName();
}
}
I apologize for the long post. My question is simply: How do I sort the contents of the ArrayList accurately? I keep getting an error saying that I'm trying to compare the wrong types with my "Collections.sort" line. If anyone can help out, please let me know. Thank you.
我为长篇幅道歉。我的问题很简单:如何准确地对 ArrayList 的内容进行排序?我不断收到错误消息,说我正在尝试将错误的类型与我的“Collections.sort”行进行比较。如果有人可以帮忙,请告诉我。谢谢你。
edit: To be clear, I'm trying to sort them alphabetically by the string stored in getName() for each object
编辑:要清楚,我正在尝试按每个对象的 getName() 中存储的字符串按字母顺序对它们进行排序
回答by Nayanjyoti Deka
Use
利用
Collections.sort(dogList, new Comparator<Dog>(){
public int compare(Dog d1, Dog d2){
return d1.getName().compareTo(d2.getName());
}
});
This will compare each dogs based on comparator. Comparator simply compares the names of dogs.
这将根据比较器比较每只狗。比较器只是比较狗的名字。
Update: For idiomatic java 8 style
更新:对于惯用的 java 8 风格
Collections.sort(dogList, (d1,d2) -> d1.getName().compareTo(d2.getName()));
回答by Vishal Gajera
kindly follow this way, don't make code too much stuff, here i see you have mess up you code and it makes difficult to understand easily.
请遵循这种方式,不要编写太多代码,在这里我看到您将代码弄乱了,并且很难轻松理解。
kindly follow this easiest way to sort Dog Object's list in sorting order(Ascending/Descending) way.
请按照这种最简单的方法按排序顺序(升序/降序)对狗对象的列表进行排序。
import java.util.ArrayList;
import java.util.Collections;
class Dog implements Comparable<Dog>{
String name;
Dog(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Dog o) {
return this.name.compareTo(o.getName()); // dog name sort in ascending order
//return o.getName().compareTo(this.name); use this line for dog name sort in descending order
}
@Override
public String toString() {
return this.name;
}
}
public class DogSort {
public static void main(String[] args) {
ArrayList<Dog> listDog = new ArrayList<Dog>();
listDog.add(new Dog("doggy"));
listDog.add(new Dog("aaa"));
listDog.add(new Dog("bbb"));
System.out.println(listDog);
Collections.sort(listDog);
System.out.println(listDog);
}
}
回答by Abdelhak
You can sort it with:
您可以使用以下方式对其进行排序:
Collections.sort(list);
回答by ifly6
You'll need to implement the Comparator interface.
您需要实现 Comparator 接口。
回答by guillaume girod-vitouchkina
You use the same name DogList, for your class and the variable. Change it.
您为您的类和变量使用相同的名称 DogList。更改。
For example:
例如:
private ArrayList<Dog> innerList;
in your DogList class, declare a sort method
在 DogList 类中,声明一个排序方法
public sort() {
Collections.sort(innerList);
}
and your Dog class must be comparable
并且您的 Dog 类必须具有可比性
public class Dog implements Comparable {
public int compareTo(Object _other)
{
// DO the comparaison
return 1;
}
}
回答by Ali
You can use Comparator to solve this ..
您可以使用 Comparator 来解决这个问题..
I hope this will solve your issue - How to sort an ArrayList in Java
我希望这能解决您的问题 -如何在 Java 中对 ArrayList 进行排序
回答by Amit
Either implement the Comparable interface in your Dog class or write a custom Comparator which would sort Dog on their name.
要么在你的 Dog 类中实现 Comparable 接口,要么编写一个自定义的 Comparator 来根据他们的名字对 Dog 进行排序。