Forums Register Login

Collections.sort(list, comparator) doesnt work

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Timeout.
You can't compare objects with < - it's for comparing primitive numerical types only.
+Pie Number of slices to send: Send
 

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.
permaculture is largely about replacing oil with people. And one tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1249 times.
Similar Threads
Sorting ArrayList of MemberBean objects
sorting an ArrayList Alphabetically
String Numeric Comparator
Order a collection by date
Help-> sorting different table columns
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:59:11.