• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with sorting objects  RSS feed

 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to sort objects and have an index pointer. Here is a sample code that produces results that are not sorted. What am I doing wrong?



Output is shown below

 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us what the app does. Write it down in words of one syllable. What do you use to sort on? Why do you sort on Integers?
That will be far more useful than our trying to go through a main method which is far too long.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a collection of objects that can be accessed via and index, i.e. collection.get(index). I would like to sort an array of integers (indices) that point to the collection, so that the indices come out in order such that the following are in decreasing order collection.get(indices.get(1)) >= collection.get(indices.get(2)) >= collection.get(indices.get(3)), etc. So, I'm trying to sort based on indices, and leave the collection alone.

The following code works, but uses an auxiliary class to hold the collection and sort based on the indices. The original question used a "implicit class member" declaration on the collections.sort command. The question is why doesn't the implicit class formulation work?



Correct output



If the question is not clear, please ask a question about the information presented.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!