如何按名字,姓氏等对人员列表进行排序?

时间:2014-12-18 09:59:30

标签: java

我提示用户输入他的名字,姓氏和年龄 我使用确认对话框询问用户是否要添加其他人,然后显示他输入的姓名。

如何按名字,姓名等对人进行排序?

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;
    }


}

3 个答案:

答案 0 :(得分:7)

执行此操作的两种方法是:使用人工具Comparable或使用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);
        }
});

(只需在你的for循环之前添加它)

您现在可以自己弄清楚如何使用Comparable

答案 1 :(得分:1)

如果您使用的是Java 8,则可以使用Comparator.comparing指定要用于比较的方法,并thenComparing指定在绑定时使用的方法,等等。< / p>

Collections.sort(persons, Comparator.comparing(Person::getfName)
                                    .thenComparing(Person::getLname)
                                    .thenComparing(Person::getAge));

使用相同的方法使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);
    }

}

答案 2 :(得分:0)

连接名称的所有部分并使用它们进行排序。在Person类中实现Comparable接口。

public class Person implements Comparable<Person>{
...
    int compareTo(Person o){
         return (fName + lname).compareTo(o.getfName() + o.getLname());
    }
...
}

然后您可以将此类对象的列表传递给Collections.sort。如果您可以访问Person的类代码,那么使用Comparable接口会更好。当您使用第三方库并且无法添加新接口的实现时,不同的比较器非常有用。