Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldnt I have to call a swap() somewhere?
 
Marco Masi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you make the database do the sorting for you? Simply specify an ORDER BY clause.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic