• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing the java.util.Comparator

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see two problems:
1) In compare method, every reference to the o1 and o2 must be cast to Clock. You're only doing it once.
2) You should be using the static method: Arrays.sort(Object[], Comparator) method.
 
bapi dhar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear friend
thanx
bapi
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bapi,
when you post source code, it makes it much more readable to
place it within the CODE and /CODE delimiters. you've gotta
put []'s around the words CODE AND /CODE. see:
http://www.javaranch.com/ubb/ubbcode.html
it makes text look like
ps. looks like you and i are both on the same page of van
der linden's "just java".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic