aspose file tools*
The moose likes Beginning Java and the fly likes Sort Array Of Objects Based On A Specified Property Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sort Array Of Objects Based On A Specified Property" Watch "Sort Array Of Objects Based On A Specified Property" New topic
Author

Sort Array Of Objects Based On A Specified Property

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
How would I go about sorting an array of objects based on a specified property. For instance, if I had an array of movie objects called Movie[] movies, and the movie class had the properties String title, int popularity, and Date releaseDate, how would I go about writing methods that sort the array based on either title, popularity, or releaseDate?

For instance, I have this class which compares the releaseDate of two rentals:


I also have a compareTo method in the Rentals class (which is the superclass for Movies and implements Comparable) which compares two strings(titles).

How do I implement Arrays.sort for these two types of comparison methods?

Thanks!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Well, if I'm not mistaken, the DateComparison class will work perfectly fine for Movie as well, since Movie is a subclass of Rental. You can just call the Arrays.sort() method with the Movie array, and the DateComparison Comparator.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
So would I write it likes this:
Arrays.sort(movies, DateComparison())

And then how would I call Arrays.sort for my compareTo and compare methods?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

No, Arrays.sort(movies, new DateComparison());

I don't understand what you mean by your last question. Which compareTo/compare methods? What problem are you having?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:
And then how would I call Arrays.sort for my compareTo and compare methods?


You just pass the array and the Comparator to the sort() method, as Stephan just showed you. You don't have to do anything else for the compare() method to be called. The sort() method calls it.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
These methods which are in my Rental class.


How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Kd Martin wrote:How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort


"kd", please read the last two replies by Stephan and Jeff. They provide the complete answer. While reading them you should be aware that you are making some incorrect assumptions about the process, so be prepared to recognize them as you read.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Arrays.sort will use the compareTo method of its elements if you don't provide a Comparator. If you do, it will use the compare method of the comparator. That's how it works. To be able to sort on another field you just create a new Comparator.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39804
    
  28
This sounds like the sort of thing which a database should do; you can search for particular features in the database.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:These methods which are in my Rental class.


Yes, you already told us that.

How do I call them in the Arrays.sort. Is it Arrays.sort(movies, compare())? I'm having trouble understanding how to call these methods as a parameter in the Arrays.sort


That question was just answered a few posts ago. Simply re-asking the same question won't get you anywhere. If you didn't understand the answers, ask for clarification. Make sure to be as specific as possible about what parts of the answers you didn't understand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort Array Of Objects Based On A Specified Property