| Author |
Sorting List By Comparator
|
nazeer hussain
Greenhorn
Joined: Feb 06, 2006
Posts: 21
|
|
Hi I have a code to sort the object list by a specific attribute as follows. public GenericComparator(String field, String order) { super(); this.fieldName=field; if (order == null || order.equals(ASC)){ this.sortOrder=ASC; } else if (order.equals(DESC)){ this.sortOrder=DESC; } } public int compare(Object o1, Object o2) { Object value1; Object value2; try{ Field f = this.getAccessibleField(o1); if (f == null) return 0; if (this.sortOrder.equals(ASC)){ value1 = f.get(o1); value2 = f.get(o2); } else { value2 = f.get(o1); value1 = f.get(o2); } return this.compareField( value1, value2); }catch (Exception nsfe){ System.out.println(nsfe); return 0; } } But I want to sort the objects after calculating a ratio of two different attributes in the objects in object list. Please give me an idea. Thanks in advance Nazeer
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32685
|
|
You have already had a useful reply on the beginners' forum. A Comparator doesn't return a ratio, unless you have done lots of arithmetic in it.
|
 |
 |
|
|
subject: Sorting List By Comparator
|
|
|