Thursday, July 29, 2010

All about comparators

 package mysorting;

import java.util.Comparator;

public class ComparePersonByName implements Comparator
{

    @Override
    public int compare(Object p1, Object p2) {
        Person person1 = (Person)p1;
        Person person2 = (Person)p2;
        String s1 = person1.getName();
        String s2 = person2.getName();
      
        return s1.compareTo(s2);
    }

}

package mysorting;

import java.util.Comparator;

public class ComparePersonByAge implements Comparator
{

    @Override
    public int compare(Object p1, Object p2) {
        Person person1 = (Person)p1;
        Person person2 = (Person)p2;
        Integer age1 = person1.getAge();
        Integer age2 = person2.getAge();
       
        return age1.compareTo(age2);
    }

}

package mysorting;

import java.util.Comparator;
import java.lang.reflect.*;

public class ComparePersonByNameDescending implements Comparator
{

    @Override
    public int compare(Object p1, Object p2) {
        Person person1 = (Person)p1;
        Person person2 = (Person)p2;
        String s1 = person1.getName();
        String s2 = person2.getName();
       
        return - s1.compareTo(s2);
    }

}


package mysorting;
public class Person implements Comparable
{
    // fields
    private String name; // the person's name
    private int age;
    public int getAge() {
        return age;
    }

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

    private int maximumBooks;
  
    // constructor
    public Person(String n)
    {
        name = n;
        maximumBooks = 3;
    }
   
    public Person(String n, int a)
    {
        name = n;
        age = a;
        maximumBooks = 3;
    }   
   
    // getters and setters
    public String getName()
    {
        return name;
    }
  
    public void setName(String n)
    {
        name = n;
    }

    public int getMaximumBooks() {
        return maximumBooks;
    }

    public void setMaximumBooks(int maxBooks) {
        maximumBooks = maxBooks;
    }
   
    public String toString()
    {
        return getName() + " (" + getMaximumBooks() + " books)";
    }

    @Override
    public int compareTo(Object arg0) {
        Person p1 = (Person)arg0;
        if (name.compareTo(p1.getName()) == 0 )
            return 0;
        else if (name.compareTo(p1.getName()) < 0)
            return -1;
        else if (name.compareTo(p1.getName()) > 0)
            return +1;
        return 0;
    }
}
  

package mysorting;

import java.util.Comparator;

public class Sorting
{
    public static void BubbleSort(Person a[], Comparator c)
    {
        for (int i = 0; i < a.length - 1; i++)
        {
            for (int j = 0; j < a.length - i - 1; j++)
            {
                if (c.compare(a[j], a[j+1]) > 0)
                {
                    Person temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                } // end if
            } // end for j
        } // end for i
    } // end method Bubblesort
   
    public static void main(String args[])
    {
        Person a[] = new Person[4];
        a[0] = new Person("mary", 12);
        a[1] = new Person("jeremy", 10);
        a[2] = new Person("susan", 15);
        a[3] = new Person("josh", 20);
       
        Sorting.BubbleSort(a, new ComparePersonByNameDescending());
        for (Person x : a)
            System.out.println(x);
       
        System.out.println("");
       
        Sorting.BubbleSort(a, new ComparePersonByAge());
        for (Person x : a)
            System.out.println(x);
    }
}

No comments:

Post a Comment