• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort Question  RSS feed

 
Vivek V Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a list of Integers and another list/set with all the unique elements in the original list. I need to sort the unique list/set on the basis of the number of occurrences of each of its elements in the original list. For example:

originalList = {aa,b,cc,ddd}
uniqueList = {a,b,c,d}
sortedUniqueList = {d,a,c,b} (the elements a and c both occur twice in the original list, and can be interchanged)

Can anyone help me out? Thanks a lot in advance!
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need a method which returns the number of occurrences of an element in the original list. Then write a Comparator which calls that method for the two elements it is comparing and uses the results appropriately. Finally, use a version of the Collections.sort method which uses an instance of that Comparator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!