wood burning stoves 2.0*
The moose likes Java in General and the fly likes writing generic sort method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "writing generic sort method" Watch "writing generic sort method" New topic
Author

writing generic sort method

Lido Scaggsinsky
Greenhorn

Joined: Sep 16, 2007
Posts: 9
I'm trying to write a static sort class (for practice, I know that there is one already) that takes any ArrayList of comparable objects and sorts it. I've written a generic binary search tree before, but can't seem to get this started.


Is generic types even applicable here? I was also thinking that it could just take an ArrayList of Objects and that object's comparator so it could do the sort on different fields, but I'm not sure how to start that either. It's been a couple of years since I worked with this stuff and I'm trying to brush up, any help would be appreciated. Thanks.
[ September 16, 2007: Message edited by: Lido Scaggsinsky ]

Lido
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Lido Scaggsinsky:]for practice, I know that there is one already

A comparison function, which imposes a total ordering on some collection of objects, can be passed to a sort method Collections.sort to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures such as, TreeSet or TreeMap. This comparison method is placed in your class from Comparator.java describing the public interface java.util.Comparator<T> This generics stuff, I do not have a good handle on but always employs that double diamond notation, which allows the compiler to do compile time type checking. There is no RTTI in Java to speak of. ( and good riddiance in my mind right at the moment - though I will cycle through that again and again )



Which are described in C:\src_jdk1.5.0_12\java\util\Comparator.java

These methods have to be implemented in whatever it is you are trying to sort. If you have the methods written correctly, TreeMap and TreeSet will do the Red-Black implementation for you.

If I may be bold, visit:

http://math.hws.edu/javanotes/

skip to the Collections from the index page and you'll be coding before Sunrise.



[ September 16, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Nicholas Jordan:
There is no RTTI in Java to speak of.


Er, what?

I presume what you mean is that Java erases generic types during compilation and hence generic type information is not available at run-time. But Java has a large amount of run-time type information available, via Reflection, instanceof etc. etc.

I don't know if you're thinking of C++ RTTI. If so, C++ without RTTI really does have no run-time type information at all. I don't know whether C++ with RTTI has run-time generic type information - does it? But Java has infinitely more run-time type information than C++ without RTTI.
[ September 17, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Peter Chase:]Er, what?

Well, I was trying to keep my post from running 1-10 k, which is not unusual for me. Shortly after the post I went looking in Class.java

To be sure, the RTTI ( in java ) is sophisticated, but consider the type resolution of an n-layered inheritance ( replete with changes in decision making to go along with it ) such as that proposed by Pat Farell:

Generalized solution to instanceof

I am still working on this concept challenge, from ai we know that if the number of dimensions in an n-dimensioned rises, the problem complexity rises at an exponential. IOW - beginner's all night hair puller on the 80-core thread allocation problem and maybe it will do some good, but what are you asking me ?
[ September 17, 2007: Message edited by: Nicholas Jordan ]
michael warren
Greenhorn

Joined: Mar 04, 2004
Posts: 8
I think you want a method something like this


its a bit beyond what I've used generics for so was intersting to me to try it out - the following code may well have lots of things wrong with it as I've cobbled it together quickly from various sources, but it seems to work

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing generic sort method
 
Similar Threads
Bounded generic method
Inner class usage
Need Help Sorting ArrayList of Generic Objects
Collections.sort and generics
generics/newInstance of Class<T>