This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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 ^__^)
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.
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
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.