• 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

Collections.sort(list, comparator) doesnt work

 
Ugly Redneck
Posts: 1006
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I have created a Comparator and I have a list. When I use the static method in Collections, called sort.. the sort works sometimes but on other occasions does not.
That is it sorts as though 108 < 12 is true
Can anyone explain why?
Thanks
SR
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post your comparator code. Just from looking at your current results, it looks like it is comparing objects lexicographically ( like comparing Strings ) rather than as a number value.
Jamie
[ August 29, 2002: Message edited by: Jamie Robertson ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that your comparator needs work. Post the code and we'll take a look.
You might want to take a look at this thread:
https://coderanch.com/t/355091/Servlets/java/order-DB-results-servlets
 
Paul McKenna
Ugly Redneck
Posts: 1006
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
Thanks a lot to both of you. I did find the mistake. You see the Comparator was working fine, but like Jamie pointed out, the List had string objects in it. And in String comparisons, 108 < 12 is true.
I rectified that by changing such objects to integer and it worked fine.
Thanks a lot again.
SR
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Timeout.
You can't compare objects with < - it's for comparing primitive numerical types only.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
Timeout.
You can't compare objects with < - it's for comparing primitive numerical types only.


I don't think he was doing a < directly. He was probably using the string compare method which does a < == > compare.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic