aspose file tools*
The moose likes Beginning Java and the fly likes Beginner's homework - sorting Arraylists of objects based on parameter properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner Watch "Beginner New topic
Author

Beginner's homework - sorting Arraylists of objects based on parameter properties

Alexander Thompson
Greenhorn

Joined: Dec 06, 2012
Posts: 1
Hi guys,

Having no previous programming experience whatsoever (Philosophy & Linguistics student), I've decided to take a bunch of intro to programming and Java classes. I'm at the stage where I can read code with some degree of success, but having really hard time writing something from scratch.

Currently working on a problem which is ought to simulate a bookstore, apparently - I've created a few classes and extended them to subclasses, implemented a few methods, etc. It's going good apart from this one thing.

Apologies if it's very basic (which I suspect it is!), here's the gist of it:


...


...

So here is the approximate output.

Name: Book1
Amount of pages: 256
Genre: Comedy
Coated: true
..other parameters

Name: Book2
Amount of pages: 347
Genre: Romance
..other parameters

Name: Book3
Amount of pages: 85
Genre: Historical
Coated: false
..other parameters



So the next step is to construct a method (ShowByGenre) to sort my books by genre (all comedies, then all autobiographies, etc). I have a vague idea how to implement this, but not sure what should I type in first.

ArrayList<Book> BookShelf = new ArrayList<Book>();
BookShelf.add(book1);
BookShelf.add(book2);
BookShelf.add(book3);


System.out.println( "Genre - Comedy: " );
Book.BookGenre genre;
genre = Book.BookGenre.Comedy;
ShowByGenre(BookShelf, genre);


System.out.println( "Genre - Romance: " );
Book.BookGenre genre;
genre = Book.BookGenre.Romance;
ShowByGenre(BookShelf, genre);


etc.
Thanks for your input!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There are a couple ways to go about this, depending on what the requirements are. In a real world situation I would create a java.util.Comparator that could compare two book's Genre element, then I would use java.util.Collections.sort(List, Comparator) to do the sorting for me. In this case, you should read the Object Ordering Tutorial to help you learn how to use Comparables and Comparators.

But that is a real-world case where you want to rely on as much work done by other people as you can. In a learning environment you might be required to implement your own sorting algorithm. If that is the case - there is a lot on the net about the various algorithms. Your instructor probably told you the name(s) of one or more algorithm, and a little searching should help you figure out what to do. In your specific case, since you are sorting the Genre, for each book you would get the Genre's 'ordinal' - or its order in the BookGenre Enum, and use that for comparison. As a brief glimpse into what it might look like:


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner's homework - sorting Arraylists of objects based on parameter properties