• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Trouble with sorting objects  RSS feed

 
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

 
Marshal
Posts: 59745
188
  • 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!