• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How does Collections.sort() work

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How does Collections.sort work?

I got a list of raw data which is ordered properly, and a comparator which has a list of code is sorted against.

Collections.sort(rawDataList, codeSortList)

when i run this I get a result that:
it is sorted against the code properly( which i want) but within each code sorted, the list is not ordered properly.

Thanks in advance

Anil
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by AnilPrakash Raju:
Hi,

How does Collections.sort work?



See The API

Originally posted by AnilPrakash Raju:
I got a list of raw data which is ordered properly, and a comparator which has a list of code is sorted against.

Collections.sort(rawDataList, codeSortList)

when i run this I get a result that:
it is sorted against the code properly( which i want) but within each code sorted, the list is not ordered properly.



The sort will sort only to the comparisons defined by the Comparator you define. If your comparator sorts on codes and not to any value within the code group, then the result list will be sorted by code only.

For example, if I had a list of names that were comprised of First Name, Last Name fields, and my Comparator sorted by Last Name:


In this case, since I am ordering by Last Name only then there would be no guarantee that Adam Luke would come before Steven Luke. Both have the same Last Name so they are equal as far as your sort is concerned.

To fix this, I would have to modify the Comparator to first compare Last Names, and if the result of the comparison was 0 (they are equal) then compare First Names and return the result of that.



You will have to do something similar with your program. First compare codes, and if the codes are equal compare again by whatever parameter sorts within the code.
 
It will give me the powers of the gods. Not bad for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic