Reputation: 1
I've started programming recently so this could turn be easy. I'm having a difficult in order my object. I have a object with Name, Birth Date and phone number. Like Object[0][0]="John" Object[0][1]="10/10/2010" Object[0][0]="900000000" with several other persons. So my difficult is to sort by name and keep the Birth date and phone number along with the name. Thank you.
Upvotes: 0
Views: 108
Reputation: 324147
Here is my standard sorting example, which happens to use a Person object:
/*
** Use the Collections API to sort a List for you.
**
** When your class has a "natural" sort order you can implement
** the Comparable interface.
**
** You can use an alternate sort order when you implement
** a Comparator for your class.
*/
import java.util.*;
public class Person implements Comparable<Person>
{
String name;
int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name + " : " + age;
}
/*
** Implement the natural order for this class
*/
public int compareTo(Person p)
{
return getName().compareTo(p.getName());
}
static class AgeComparator implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getAge() - p2.getAge();
}
}
public static void main(String[] args)
{
List<Person> people = new ArrayList<Person>();
people.add( new Person("Homer", 38) );
people.add( new Person("Marge", 35) );
people.add( new Person("Bart", 15) );
people.add( new Person("Lisa", 13) );
// Sort by natural order
Collections.sort(people);
System.out.println("Sort by Natural order");
System.out.println("\t" + people);
// Sort by reverse natural order
Collections.sort(people, Collections.reverseOrder());
System.out.println("Sort by reverse natural order");
System.out.println("\t" + people);
// Use a Comparator to sort by age
Collections.sort(people, new Person.AgeComparator());
System.out.println("Sort using Age Comparator");
System.out.println("\t" + people);
// Use a Comparator to sort by descending age
Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
System.out.println("Sort using Reverse Age Comparator");
System.out.println("\t" + people);
}
}
Upvotes: 0
Reputation: 393956
The logical thing to do would be to create a Person
class that contains name, birth date and phone number. Then, instead of using a 2D Object array, which is not type safe, use a 1D array - Person[]
.
If your Person class would implement Comparable<Person>
(with comparison logic that compares the names), Arrays.sort()
would sort the array for you by name.
Upvotes: 2