• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to sort an array Strings using Collections.sort  RSS feed

 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have an array of Strings like below


Below is the actual result I have, as you see the values are not sorted.
[10, 1022, 1034, 1035, 107, 11, 1335, 17, 178, 179, 180, 199, 20, 2397, 31, 401, 415, 417, 418, 419, 420, 422, 423, 425, 426, 428, 433, 454, 457, 458, 460, 462, 47, 475, 48, 50, 5040, 51, 52, 53, 55, 56, 57, 58, 59, 596, 613, 621, 623, 624, 630, 631, 675, 676, 677, 678, 680, 681, 682, 683, 690, 693, 695, 698, 712, 719, 74, 761, 83, 84, 85, 864, 889, 92, 97, 98]

I'm not sure, if I'm doing anything wrong here. I'm assuming that the values being String, should have been sorted by its natural order.
 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,

are you sure they aren't sorted?

;)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result is properly sorted.

For example, the String "1335" is less than the String "17", for the exact same reason that the String "ACCE" is less than the String "AH". The sort routine has no way of knowing that you want to interpret those Strings as numbers.

If you want those Strings to be interpreted as numbers, and then sort the array by the numerical values of the Strings, you'll need to supply a Comparator that does Integer.parseInt() (or whatever conversion to number is appropriate for your needs), compares the resulting number values, and then returns negative, zero or positive as appropriate.

Or you could just put numbers into the array to start with.

I'm assuming that the values being String, should have been sorted by its natural order.


That assumption is correct, and that's how the Strings were sorted.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Raja wrote:Hi All,

I have an array of Strings like below


Below is the actual result I have, as you see the values are not sorted.
[10, 1022, 1034, 1035, 107, 11, 1335, 17, 178, 179, 180, 199, 20, 2397, 31, 401, 415, 417, 418, 419, 420, 422, 423, 425, 426, 428, 433, 454, 457, 458, 460, 462, 47, 475, 48, 50, 5040, 51, 52, 53, 55, 56, 57, 58, 59, 596, 613, 621, 623, 624, 630, 631, 675, 676, 677, 678, 680, 681, 682, 683, 690, 693, 695, 698, 712, 719, 74, 761, 83, 84, 85, 864, 889, 92, 97, 98]

I'm not sure, if I'm doing anything wrong here. I'm assuming that the values being String, should have been sorted by its natural order.



And that is how it is sorted -- it is sorted using the natural order for strings. Just like "Category" comes after "Cat", but before "Dog", "1022" comes after "10", but before "50".

[EDIT: Beaten to the answer again... ]

Henry
 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah,... my bad. Though I'm seeing them as String, in some corner I was expecting them to behave as numbers.. how stupid.. ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!