jQuery in Action, 3rd edition
The moose likes Programming Diversions and the fly likes CODE: Dynamic Sorting on Multiple Indexes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "CODE: Dynamic Sorting on Multiple Indexes" Watch "CODE: Dynamic Sorting on Multiple Indexes" New topic

CODE: Dynamic Sorting on Multiple Indexes

Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
Here's a handy utility called “DynamicComparer”, that will dynamically sort an array or ArrayList on multiple fields, in ascending or descending order, without or without case sensitivity.

Using this, it is no longer necessary to allow sorting by having classes implement the Comparable interface, or writing custom Comparators for each business class.

I welcome feedback! Let me know if you think of any improvements.


Code here, in filename DynamicComparer.java

JUnit test here, in filename DynamicComparer_Test.java
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

"Kamilche Unknown", please check your private messages for an important administrative matter.

[Asking smart questions] [About Bear] [Books by Bear]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Thank you for posting this. I ran 2 tests. The first gave a runtime error and the second did not really perform:

Gave: Exception in thread "main" java.lang.RuntimeException: Field 'name' not found in class 'Test'!

Time: 38
Time: 0

That is a big difference and to much in most cases.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
Thanks for the feedback. You got that error is because the field is not public - comparators can only access public fields.

You were spot on about the speed, though! Wow, it's abysmal - 20 to 70x slower than a normal comparator. I added a 'testCapacity' routine to the JUnit tests, and discovered what you meant.

I tried some different methods, and came up with this one. It is flexible, in that it will let you sort on multiple columns with different sort criteria, like DynamicComparer... but it performs much faster, and scales well. It also allows you to access private fields.

It's still slower than a custom comparator, so if you have a need for a one off fast sort, hand coding is probably the best way to go. This routine is mainly useful for small arrays, such as you might use during web development, when you're presenting a limited result set to the user, letting them choose their sort columns, and don't want to have to hit the database every time they click the sort button.

Give this one a whirl, I think you'll like it better. My timings are below:

  • Standard method: Sorted 100000 items in 47 milliseconds. This is the usual Java method, where you create a new custom comparator for each sort you want.
  • New method: Sorted 100000 items in 172 milliseconds. This is the new method I created, which uses a single custom comparator that handles all possible sorts you might want, with per-field direction and case sensitivity.
  • Lazy method: Sorted 100000 items in 3437 milliseconds. This method is like 'new', but calls a pre-done compareTo proc to get you out of handing-coding the compareTo proc. The pre-done compareTo proc uses reflection to look up the fields, so while it saves you coding work, this is the slowest method.

  • File MultiComparable.java

    File MultiComparer.java

    File TestClass.java

    File MultiComparable_Test.java

    I agree. Here's the link: http://aspose.com/file-tools
    subject: CODE: Dynamic Sorting on Multiple Indexes
    It's not a secret anymore!