Say I have a Song object, for example
public Song(){ String artist, title; StringBuilder lyrics; int rank; }
Is it possible to have several comparison methods that, depending on the collection used, are sorted by a specific field? This object already has a comparison method for ordering based on the values โโof the artist and name, and I would like to be able to order on the basis of rank.
In my current project, we need to start a search for a Song song and return a high to low list. I want to use PriorityQueue to store matches based on rank value.
Usually I would simply create another object to store the song and rank, but this project not only connects to the GUI interface provided by the professor, which requires that any results be transferred in the Song [] array, but print the first ten values โโas Rank, Artist, Title.
I can use toArray () to convert the queue, but if I use it to store anything other than Song objects, it will throw an ArrayStoreException.
So, is this possible, or do I need to modify an existing comparison method to sort by integer value?
Jason source share