• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting two Classes

 
hans meiser
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i have the problem, that i want to merge two lists of different classes to one list and then i want to sort this list.
After sorting i have to know which class the elements belong to and use different methods for each element.
If i merge two lists, how can i get out the types (classes) of the objects. A ArrayList just returns Objects. How can i cast them to the original class.
Anyone any idea?? Or some code to look how to do?
Thanks in regard
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Options:
1) have them both implement an interface
2) use either object.getClass() or instanceOf
3) use reflection to choose and execute the required code
I usually go for the Interface solution, gather the common functionality into an interface of common operations (they can both be compared for sorting and they can be executed) then sort and execute a bunch of interfaces rather than object instances.
Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic