It's not a secret anymore!*
The moose likes Java in General and the fly likes Sorting an ArrayList<MyObject> by a member of said MyObject Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting an ArrayList<MyObject> by a member of said MyObject" Watch "Sorting an ArrayList<MyObject> by a member of said MyObject" New topic
Author

Sorting an ArrayList<MyObject> by a member of said MyObject

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Hello everyone.

I have an ArrayList<> which holds objects. These objects contain among other things java.sql.Date() fields. I want to sort the ArrayList by this field. Does anyone know the best way to go about doing this?

Thank you in advance,


Greg
Marco Masi
Greenhorn

Joined: Apr 18, 2009
Posts: 7
Mmm the easiest way is use Collections utility and one of two:
implements Comparable it that sorting is the natural sorting of you object and use Collections.sort(List<T>)
write a custom Comparator and use Collections.sort(List<T>,Comparator<T>)

A comparator could be something like that (Date is already comparable ^__^)


Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

wouldnt I have to call a swap() somewhere?
Marco Masi
Greenhorn

Joined: Apr 18, 2009
Posts: 7
You can call swap if you just need to... swap ( ) two known elements. If you need to sort the entire list just use sort (with or without comparator as I wrote before). You could write your own sorting algorithm but I wouldn't do it if not in real special cases, it handle sorting quite well.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I started to try to build that comparitor as per your code, but i cannot find Comparator in the libraries. Is that a standard class?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Greg Reeder wrote:I started to try to build that comparitor as per your code, but i cannot find Comparator in the libraries. Is that a standard class?

java.util.Comparator
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I finally figured it out. Thank you for all of your help. Rather than making a new class as per your example, I was able to just use an interface and override 1 method. The was done right where i needed it upon retrieving the arraylist from the db. Thanks again

Greg
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Can't you make the database do the sorting for you? Simply specify an ORDER BY clause.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I could. But that would require making another method in my DBHelper object. i already have the list (sorted the way I want it from the DB), and this is just 1 special case. I figured it would be faster, not to mention easier, to just use a sort function with an already existing arraylist. At any rate, this issue is now solved. on to the next beast -- adding a column with calculated values in my JTable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an ArrayList<MyObject> by a member of said MyObject
 
Similar Threads
How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc
Stratergy vs Factory method vs Template method patterns
Map key or key in object?
ArrayList class
getting the next element from a arraylist