• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to sort a List of Objects in ascending order based on an enum using Java?  RSS feed

 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say you have a particular enum called Field:


And you have a corresponding Java interface called Music


And you implement it like this:

Does this sound correct?


Is this a correct way to implement the sortAlbums method?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm struggling to see what your getAlbum() method is supposed to do here. It just calls getField(Field.ALBUM). That returns field.getClass(). Which means that getAlbum() will always return exactly the same thing, which is the Class object representing Field (i.e. Field.class). I'm pretty sure that isn't what you intended.

A couple of other tips:

- There's a Collections.sort() method as well, you don'y need to convert it to an array and back again.
- Line 6 of MusicManager has an error which means you will always be trying to sort in ascending order!
 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Field is an enum... Am new to this and the interface predefined getField() method.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!