0

所以我试图弄清楚这一点。我创建了一个包含年龄的名称数组列表,必须按年​​龄排序,然后按名称排序(如果年龄相等)我确信有一种简单的方法可以做到这一点,但我们的课程要求我们使用接口。所以到目前为止,我所拥有的是一个包含人员姓名和年龄的数组列表,然后是一个人员类,其中包含我可以从中检索信息的方法。如何对要传递回我的主类的列表进行排序?人员分类器:

import java.util.ArrayList;
import java.util.Collections;


public class PersonSorter
{
    public static void main(String[] args)
    {
        ArrayList<Person> people = new ArrayList<Person>();

        people.add(new Person("Linda", 63));
        people.add(new Person("Jacob", 5));
        people.add(new Person("Emily", 13));
        people.add(new Person("Jessica", 21));
        people.add(new Person("Emma", 5));
        people.add(new Person("Robert", 80));
        people.add(new Person("Jennifer", 43));

        // PRINT THE LIST OF PEOPLE BEFORE SORTING
        for (Person person : people)
        {
            System.out.println(person);
        }

        System.out.println();//space between the lists

        Collections.sort(people);

        // PRINT THE LIST OF PEOPLE AFTER SORTING
        for (Person person : people)
        {
            System.out.println(person);
        }
    }
}

人:

public class Person implements Comparable<Person>
{
    /** The person's name */
    private int age;

    /** The person's age */
    private String name;

    /**
     * Constructs a new Person object with the given name and age
     * 
     * @param age of the person
     * @param name of the person
     */
    public Person(String name, int age)
    {
        this.age = age;
        this.name = name;
    }


    /**
     * 
     * Returns the age of the person 
     *
     * @return age
     */
    public int getAge()
    {
        return age;
    }


    /**
     * 
     * Sets the age of the person 
     *
     * @param age
     */
    public void setAge(int age)
    {
        this.age = age;
    }


    /**
     * Returns the name of the person
     * 
     * @return name
     */
    public String getName()
    {
        return name;
    }


    /**
     * 
     * Sets the name of the person
     *
     * @param name
     */
    public void setName(String name)
    {
        this.name = name;
    }



    @Override
    /**
     * Returns a string representation of the person in the form:
     * Person[name = [name], age = [age]]
     */
    public String toString()
    {
        return "Person [name=" + name + ", age=" + age + "]";
    }


    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(Person o)
    {

        return 0;
    }

}

人们:

这是我将拉出数组列表并按年龄排序的类,如果需要,则按名称排序。

4

4 回答 4

5

你已经完成了大部分工作。只需在您的Person类中实现compareTo :

@Override
public int compareTo(Person o) {
   if (this.age != o.age) {
      return this.age < o.age ? -1 : 1;
   }
   return this.name.compareTo(o.name);
}

方法Collections.sort根据compareTo方法提供的顺序对项目进行排序。

于 2013-04-07T16:32:57.893 回答
0
public int compareTo(Person p) {

        return this.age - p.age;

    }

    public static Comparator<Person> PersonComparator = new Comparator<Person>() {

        public int compare(Person p1, Person p2) {

            String firstPerson = p1.name;
            String secondPerson = p2.name;

            return firstPerson.compareTo(secondPerson);

        }

    };

将此代码添加到您的 Person 类:

如果您想按年龄排序,请尝试:

    Arrays.sort(people);

如果要按名称排序,请尝试:

Arrays.sort(people, Person.PersonComparator); 
于 2013-04-07T16:58:33.897 回答
0

如果您不想继续创建多个组合 Comparator ,那么您可以使用Group Comparator之类的东西。

于 2013-04-07T17:11:29.607 回答
0

Google 的 Guava 有一个非常整洁的ComparatorChain.

于 2013-04-07T17:16:06.083 回答