java 如何按名字,姓氏等对人员列表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27543814/
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 to sort a List of Persons by first name, last name and so on?
提问by Mê D ?o
I'm prompting the user to enter his first name, last name and age.
I use a confirm dialog to ask user if he want's to add another person then I display the names that he entered.
我正在提示用户输入他的名字、姓氏和年龄。
我使用确认对话框询问用户是否要添加另一个人,然后显示他输入的姓名。
How can I sort the persons by first name, second name and so on ?
如何按名字,第二名等对人进行排序?
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList();
int i = 0;
while (i == 0) {
Person p = new Person();
String fname = JOptionPane.showInputDialog("Enter First Name ");
String lname = JOptionPane.showInputDialog("Enter Last Name ");
int Age = Integer.parseInt(JOptionPane.showInputDialog("Enter your Age "));
p.setfName(fname);
p.setLname(lname);
p.setAge(Age);
list.add(p);
i=JOptionPane.showConfirmDialog(null, "Are you Want to Add other person","Confirmation",2);
}
for (int j = 0; j < list.size(); j++) {
System.out.print("First name : "+list.get(j).getfName()+"\t Last Name : " + list.get(j).getLname()+"\t Age : "+list.get(j).getAge());
list.equals(j);
System.out.print("\n------------------------ \n ");
}
public class Person{
private String fName;
private String lname;
private int age;
public Person() {
}
public Person(String fName, String lname, int age) {
this.fName = fName;
this.lname = lname;
this.age = age;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
采纳答案by Mikhail
Concatenate all parts of the name and sort using them them. Implement a Comparable interface in a Person class.
连接名称的所有部分并使用它们进行排序。在 Person 类中实现 Comparable 接口。
public class Person implements Comparable<Person>{
...
int compareTo(Person o){
return (fName + lname).compareTo(o.getfName() + o.getLname());
}
...
}
Then you can pass list of such objects to Collections.sort. If you have access to Person's class code it's better to use Comparable interface. Distinct comparator is useful when you are working with 3rd party libraries and you can't add implementation of a new interface.
然后您可以将此类对象的列表传递给Collections.sort。如果您可以访问 Person 的类代码,最好使用 Comparable 接口。当您使用 3rd 方库并且无法添加新接口的实现时,不同的比较器很有用。
回答by Xipo
Two ways of doing it are, either have Person implement Comparableor use Comparator
这样做的两种方法是,让 Person 实现Comparable或使用Comparator
Using Comparator:
使用比较器:
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String fn1 = ((Person) o1).getfName();
String fn2 = ((Person) o2).getfName();
int res = fn1.compareTo(fn2);
if (res != 0) {
return res;
} else {
String ln1 = ((Person) o1).getLName();
String ln2 = ((Person) o2).getLName();
return ln1.compareTo(ln2);
}
});
(Just add this before your for loop)
(只需在 for 循环之前添加此内容)
You can figure out how to use Comparable yourself now
您现在可以自己弄清楚如何使用 Comparable
回答by tobias_k
If you are using Java 8, you can use Comparator.comparing
to specify what method to use for comparison, and thenComparing
to specify what method to use in case of a tie, and so on.
如果您使用的是 Java 8,则可以使用Comparator.comparing
指定用于比较的方法,以及thenComparing
指定在出现平局时使用的方法,等等。
Collections.sort(persons, Comparator.comparing(Person::getfName)
.thenComparing(Person::getLname)
.thenComparing(Person::getAge));
Or make Person
a Comparable
using the same approach:
或者让Person
一个Comparable
使用相同的方法:
public class Person implements Comparable<Person> {
// ... more code
@Override
public int compareTo(Person other) {
return Comparator.comparing(Person::getfName)
.thenComparing(Person::getLname)
.thenComparing(Person::getAge)
.compare(this, other);
}
}