Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to sort by 2 constraints?
    primarykey
    data
    text
    <p>So I'm trying to figure this out. I made an arraylist of names with ages that has to be sorted by age then by name (if ages are equal) I'm sure there is an easy way to do this, but our lesson is requiring us to use interfaces. So what I have so far is an array list of people's names and ages then a person class full of methods where I can retrieve information from. How do I sort the list to be passed back into my main class? PersonSorter:</p> <pre><code>import java.util.ArrayList; import java.util.Collections; public class PersonSorter { public static void main(String[] args) { ArrayList&lt;Person&gt; people = new ArrayList&lt;Person&gt;(); 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); } } } </code></pre> <p>Person:</p> <pre><code>public class Person implements Comparable&lt;Person&gt; { /** 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; } } </code></pre> <p>People:</p> <p>This is the class where I will be pulling the array list and sorting by age then name if need be.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload