compareTo() method

N

Nishi Bhonsle

Hi:

The compareTo() method in the Comparable Interface can be used to sort objects in a collection according to integer data such as cost, age etc.

How can I sort elements of an collection based on string values such as firstname or lastname?

Cheers, Nishi.
-----------------------------------------------------------------------------------------------------------------

import java.util.*;

class Comparer implements Comparator {
public int compare(Object obj1, Object obj2)
{
String i1 = ((Person)obj1).getLastName().toString();
String i2 = ((Person)obj2).getLastName().toString();

return i1.compareTo(i2);
}

}

class Person implements Comparable {
private String firstName;
private String lastName;
private int age;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

//for sorting according to age
public int compareTo(Object anotherPerson) throws ClassCastException {
if (!(anotherPerson instanceof Person))
throw new ClassCastException("A Person object expected.");
int anotherPersonAge = ((Person) anotherPerson).getAge();
return this.age - anotherPersonAge;
}
}

public class Testing {

public static void main(String[] args) {
Person[] persons = new Person[3];

List p = new LinkedList();
persons[0] = new Person();
persons[0].setFirstName("Elvis");
persons[0].setLastName("Goodyear");
persons[0].setAge(56);

persons[1] = new Person();
persons[1].setFirstName("Stanley");
persons[1].setLastName("Clark");
persons[1].setAge(8);

persons[2] = new Person();
persons[2].setFirstName("Jane");
persons[2].setLastName("Amir");
persons[2].setAge(16);

p.add(0, persons[0]);
p.add(1, persons[1]);
p.add(2, persons[2]);

System.out.println("Natural Order");

for (int i=0; i<3; i++) {
Person person = persons;
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}

//Arrays.sort(persons);
Collections.sort(p, new Comparer());

System.out.println();
System.out.println("Sorted by age");
for (int i=0; i<3; i++) {
Person ps = (Person)p.get(i);
String lastName = ps.getLastName();
String firstName = ps.getFirstName();
int age = ps.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
}
}
 
C

Christophe Vanfleteren

Nishi said:
Hi:

The compareTo() method in the Comparable Interface can be used to sort
objects in a collection according to integer data such as cost, age etc.

What gave you that idea? You can compare anything you want in the CompareTo
method, but you have to /return/ an int.
How can I sort elements of an collection based on string values such as
firstname or lastname?

This exactly what you are doing with the

Collections.sort(p, new Comparer());

line.

What is your specific problem?

If you want to change the order a Collection is sorted, just pass another
Comparator to the sort method.
class Comparer implements Comparator {
public int compare(Object obj1, Object obj2)
{
String i1 = ((Person)obj1).getLastName().toString();
String i2 = ((Person)obj2).getLastName().toString();

The toString() calls make no sense, getLastName() is allready returning a
String.

<snip rest of otherwise fine looking code>
 
N

Nishi Bhonsle

Christophe said:
What gave you that idea? You can compare anything you want in the CompareTo
method, but you have to /return/ an int.

I guess I was missing this point.
This exactly what you are doing with the

Collections.sort(p, new Comparer());

line.

What is your specific problem?

I answered my own problem. Got it, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top