Posted by: prankster April 13, 2011
Java
Login in to Rate this Post:     0       ?        

Answer to second.
import java.util.Comparator;

class NameComparator implements Comparator
{

    public int compare(Object ratNum1, Object ratNum2)
    {

        //parameter are of type Object, so we have to downcast it to Employee objects

        String ratNum1Val = ((RatNum)ratNum1).getValue();
        String ratNum2Val = ((RatNum)ratNum1).getValue();
        if(ratNum1Val > ratNum2Val)
            return 1;
        else if(ratNum1Val < ratNum2Val)
            return -1;
        else
            return 0;
    }

}

Not sure how yourRatNumSortingTest.java,

but you can sort using this class as below
Arrays.sort(arrayOfRatNumObjects, new RatNumComparator());

 

 

Last edited: 13-Apr-11 01:42 PM
Read Full Discussion Thread for this article