Dear friend
I could not sort the array by implementing the Comparator class
Please Help.Clock.java is in current dir.
thanx
bapi
import java.util.*;
class ClockComparator implements Comparator
{
public int compare(Object o1, Object o2) //-ve < then, 0 equal to, insertion sort +ve > than
{
if ((Clock)o1.getHours() > o2.getHours() ){ return 1;}
else
if (o2.getHours()> o2.getHours()){return -1;}
/*
if (o1.getMinutes() > o2.getMinutes() ){ return 1;}
else
if (o2.getMinutes()> o2.getMinutes()){return -1;}
if (o1.getSeconds() > o2.getSeconds() ){ return 1;}
else
if (o2.getSeconds()> o2.getSeconds()){return -1;}
*/
return 0;
}
}
public class Sort
{
static Clock clock[];
static Object temp;
public static void sort(Object[] c, ClockComparator cc)
{
for (int i=0;i<c.length;i++)>
{
for(int j=i+1;j<c.length;j++)>
{
if ((cc.compare(c[j],c[i]))<1)
{
temp=c[j];
c[j]=c[i];
c[i]=temp;
}
}
}
}
public static void main(
String args[])
{
if ( args.length !=1)
{ System.out.println("Please include number of Clock times in Command Line ... Thank You");
System.exit(0);
}
int nc=Integer.parseInt(args[0]);
clock =new Clock[nc];
for (int i=0;i<nc;i++)>
{
System.out.println("Enter Clock "+(i+1));
clock[i]=getTime(String.valueOf(i+1));
}
sort(clock,new ClockComparator());
}//main
private static Clock getTime(String timeType) {
Clock t;
while(true) {
System.out.print("Enter the " + timeType + " time in HH:MM:SS, 24-hr format: ");
try { t = Clock.valueOf(SavitchIn.readLine());
break;
} catch (Throwable e) {
System.out.println(e);
System.out.println("Problem, try again!");
}
}//while
System.out.println("The " + timeType + " time is " + t);
return t;
}
}//class