my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Sorting java.lang.objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting java.lang.objects" Watch "Sorting java.lang.objects" New topic
Author

Sorting java.lang.objects

Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
Hello,

If I have created a object array from a string array, how can I compare the items in the object array?

e.g.



thanks.
[ July 27, 2006: Message edited by: Angela Truce ]
Nimchi Yung
Ranch Hand

Joined: Jan 27, 2004
Posts: 71
If you are referring sorting a String array, you could use
Arrays.sort(obj);

--Nimchi
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
I want to use my own sorting methods rather than the api's own sorts as the point of my program is to compare speeds of sorting algorithms. I just want to be able to compare more than just one type. So a string with another string, or a double with another double...etc. The only way I can see is to write individual methods for each primitive type which would not be ideal as the code will be very long.
ie.
public string[] sort();
public double[] sort();
public int[] sort()...etc

I want to be able to pass a string, double or an integer into one sorting algorigthm. Is this achieveable using objects?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
sure you can pass in raw Objects, but then you'd have to do all the type checking inside the methods.
Maybe what you really want is an untyped language like Python or Ruby, and sink with the Titanic as soon as an incorrect type appears.


42
Angela Truce
Ranch Hand

Joined: Nov 30, 2005
Posts: 47
ok, I have an idea. If I sort using strings (as its the easiest) then I could write my own sort for numeric values as when I sort doubles/ints a strings it would sort them like this:

3.2 345 34700 5.6 6.7 68

which is obviously wrong.
Does anyone know how I can start doing this?
thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Originally posted by Angela Truce:
I want to use my own sorting methods rather than the api's own sorts as the point of my program is to compare speeds of sorting algorithms. I just want to be able to compare more than just one type. So a string with another string, or a double with another double...etc. ...

Look at the way this is done in the standard API classes. Look at class java.util.Arrays, for example. It contains a whole range of sort(...) methods for different types.

Since Java 5.0 there's a better solution, fortunately: generics. Class Arrays contains this method:

public static <T> void sort(T[] a, Comparator<? super T> c)

which works on any type T. You just have to pass it a Comparator, which is the object that knows how to compare one instance of T to another instance of T.

If you've never seen generics, it can be a little difficult to understand them. You could call the above method on different kinds of arrays, for example:

Note that it is the same sort method that is used for strings as well as numbers above. The sort algorithm uses the Comparator object to compare two objects, and the sort algorithm itself doesn't need to care what kinds of objects it is sorting.
[ July 27, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting java.lang.objects
 
Similar Threads
Casting Object arrays to String arrays
Incorrect equals() implementation...
Casting Object[] to String[]
logic:iterate in struts
q on garbage collection