aspose file tools*
The moose likes Java in General and the fly likes how to sort ArrayList by multiple values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to sort ArrayList by multiple values?" Watch "how to sort ArrayList by multiple values?" New topic
Author

how to sort ArrayList by multiple values?

Daniil Sosonkin
Ranch Hand

Joined: Jan 15, 2004
Posts: 76
I have this thing where there an ArrayList of many classes. And when I add/update/remove some class in that list, I need to know from which row was an object removed/added/updated. OK, maybe that part I could do myself by implementing a sort. But next part I don't really know how or maybe its lack of coffee.

Each class has 3 values - long date, int size and float cost. At the end, the first element in the list has to have earliest date w/ the biggest size and best cost. In SQL it would look like this: ORDER BY date ASC, size DESC, price DESC.

Frankly, I have no clue how its done Maybe someone can suggest how to sort object w/ multiple criteria in general? It'd really help
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I assume you mean many instances all of the same class. If not, please elaborate.

I don't understand your first requirement. When you do a list.remove(obj) you want to know the index of the object removed? You could doTo sort on multiple attributes of the objects you can have the objects in the ArrayList implement Comparable and use java.util.Collections.sort(list) or create a Comparator and use the other form: sort(list, comparator).

Since you're posting to the advanced forum, I'll leave this as an exercise to you. It is fairly straight-forward to do a multi-column sort with the JavaDocs. If you get stuck, post again.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi,

Here is what I have tried. Hopefully its not homework.



it should work for you...its just bare backbone...you would have to fit to your application accordingly..

Regards,
Maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to sort ArrayList by multiple values?