views:

542

answers:

7

Does anyone know of a class in Java that has a list of elements, so that the elements are sortable by any of the elements members? The idea is basically to have some database table-like implementation, where you can just add fields to sort by. To illustrate:

SomeTable table = new SomeTable();
table.addField("name", String);
table.addField("age", int);
table.addField("hair color", String);
table.add(new Object[]{"Thomas", 32, "brown"});
table.add(new Object[]{"Jack", 34, "black"});

table.sort("age") would sort the table so that Thomas is the first element, while table.sort("name") would sort the table so that Jack is the first element.

I can code something like this myself (actually i have done that once), but I'm just curious to see if there's a ready library to do such things (without a database).

PS: I know that int is not a class, but that's not the point here. This example code is just here to show the kind of functionality i expect.


+4  A: 

You must consider the Collection.sort() function, with the Comparator interface. There is a very good example here

Jérôme
+1  A: 

Have a look at JXTable: http://swinglabs.org/docs/components/JXTable/tutorial.jsp

Jens Schauder
+3  A: 

The closest thing I can think of myself is the Apache Commons BeanUtils API. For example for a "student" table.

  DynaProperty[] props = new DynaProperty[]{
        new DynaProperty("firstName", String.class),
        new DynaProperty("lastName",  String.class),
        new DynaProperty("address", java.util.Map.class)

      };
    BasicDynaClass dynaClass = new BasicDynaClass("student", null, props);

Given a collection table of these student objects. We define a BeanComparator

 BeanComparator surnameComparator
        = new BeanComparator("lastname");
Collections.sort(table, surnameComparator);

Not sure if this is what you're looking for, but it seems pretty close.

Il-Bhima
Pretty much what i was looking for. Thanks!
tehvan
+1  A: 

Google Collections Library has a similar sort of thing called a Multimap. According to the API docs:

"A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values."

So you could have name as the key and call put("Thomas", 32); then put("Thomas", "brown"); etc.

If you use the TreeMultimap implementation of this interface you can pass in a Comparator for sorting / orderings.

marktucks
I don't think that's a great use of the map; what would your generic parameter V be in that case? The Multimap is effectively a Map<K,Collection<V>> with a nicer interface, whereas what the OP wants is more like Map<K,TableRow<String,Integer,String>>.
Andrzej Doyle
But doesn't a Map<K,Collection<V>> act just like a table row with a primary key, but allowing dynamic adding of elements? It may not be *exactly* what he wants, merely a suggestion for doing a similar thing using an existing library.
marktucks
+1  A: 

There exists at least one implementation of javax.sql.rowset that supports sorting.

Many Swing TableModel extensions allows sorting (I think it's the tutorial example). Do not be afraid of the "Swing" bit even if you are not doing GUI stuff. Most Swing models do not refer to "real" Swing components at all, so can be considered completely separate (other than for bad naming).

Tom Hawtin - tackline
+3  A: 

Publicobject's glazedlists has pretty much exactly what you are after. I have used it for simillar report generation requirements. It is essentially a mechanism for giving you views on an in memory list. Your views can be sorted, filtered, and generally transformed in various ways. It also has a simple update mechanism, so you can insert new items in your master list, and all filtered / sorted views will see the update event.

Traditionally it has been very popular as a backing model for Swing grids / JTables, but it is in no way tied to the Swing API and can be used anywhere you are caching a list.

When I used it we only had a few thousand objects in memory, and performance was excellent. Not sure how it scales to enormous numbers.

serg10
A: 
  • Create a class with fields named "name", "age", "hair color"
  • Put instances of that class into an ArrayList
  • Use Collections.sort() with a custom comparator that compares a particular field
Michael Borgwardt