• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive Reflection method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I need to write a method which sorts my objects. The parent object can have n levels of child data, for example, a Customer can have many Sales, each Sale can have many SalesAttributes, each SalesAttribute can have many TransactionAttribute, etc. etc. I need to display all of the data sorted; I can do a simple sort using Comparator, but there must be code written somewhere that accepts an object with n levels of child data and sorts them all, probably calling itself recursively to sort child data.

I'm not asking my fellow ranch hands to write the method for me, but a hint or a link to some code would be gratefully appreciated; somebody somewhere has written this method and I don't want to reinvent the wheel.

I want to do:
nLevelSort(myObject);

with nLevelSort method accepting objects of any class and doing:

void nLevelSort(Object myObject){
->use reflection to find fields in myObject
->if child data is found (a list or map) then
call this method recursively to sort each list,
and if an item in the list or map has child data,
sort that too, etc. etc.
}

Any help gratefully appreciated.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what determines the order? The result of toString()? Or what?
 
Pierre Coulbert
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each of the objects implements a SortableItem interface I wrote which exposes getDisplayOrder() method returning an int:


public interface SortableItem {

public intgetDisplayOrder() ;
}

So I can do:

myList.addAll(this.getCustomers());//returns list of customer objects
myList.addAll(this.getSales());//returns list of sales objects
Collections.sort(genericItems, new MyComparator());

Where MyComparator does:

public class MyComparator implements Comparator {
public int compare(Object firstObject, Object secondObject) {
int firstOrder = ((SortableItem )firstObject).getDisplayOrder();
int secondOrder = ((SortableItem )secondObject).getDisplayOrder();
return firstOrder.compareTo(secondOrder);
}
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags. It will preserve your formatting.


That's not even going to compile. You should return something like

firstOrder - secondOrder can also work - until the difference becomes too large, and the result would be smaller than Integer.MIN_VALUE. It will then "wrap around" and become positive again.


You should check out Class.getFields() and Class.getDeclaredFields() to search for Collection and Map fields. The difference between those two methods is as follows:
- getFields returns only public (perhaps also protected, can't remember) fields, declared in the class and all super classes as well
- getDeclaredFields returns all fields (including private), but only those declared in the class itself.

It would be something like this:

That loop will make sure you get all fields, also those of the super classes.

Also, isAssignableFrom will check if the type represents Collection itself, or any subinterface or class that, directly or indirectly, implements Collection.
 
Pierre Coulbert
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greatly appreciated, ty for the help!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic