Help coderanch get a
new server
by contributing to the fundraiser
  • 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

sorting a Vector

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Vector of objects. The objects have two fields one a Calender and the other a double. I want to sort on the Calender field and am trying to use a Comparator :-
The Vector data consists of VariteMetrics.
someMethod(Vector data) {
Comparator COMPAREDATES = new Comparator() {
public int compare(Object o1, Object o2){
VariateMetric vm1 = (VariateMetric) o1;
VariateMetric vm2 = (VariateMetric) o2;
return vm1.getTime().before(vm2.getTime())? -1 : vm1.getTime().after(vm2.getTime())? 1 : 0 ;
}
};
Collections.sort(data, COMPAREDATES);
}
 
John Bell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When this code is run there is ClassCastException
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are typecasting an oject or something that the comparator doesn't like (unComparable). Thus, when your method is supposed to return a comaprator, it dishes out the class cast exc.
Or the method that you are calling is not compiling at runtime due to a method call that returns a value that is not accepted by the other method making the call.
For instance:
public Marble getMarble(){return m};
public Object getDiameter(){
//if this method returns an Oject
then the typecast to a marble object is required.
this is where your's is hanging up:
VariateMetric vm1 = (VariateMetric) o1;
VariateMetric vm2 = (VariateMetric) o2;
o1 and o2 are at runtime not comparable.
[ April 22, 2004: Message edited by: Gabriel White ]
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic