GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Sorting array's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting array Watch "Sorting array New topic
Author

Sorting array's

Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Hey yo,

I am sort of new to array's, i know how to use them and such but I have not had much like sorting them, I have a custom object of Reverse which is an array containg a String and an Int, e.g

Key Value
a 1
b 2
c 4
d 3

The way I wish to sort them is in decesding order of the Value's e.g

Key Value
c 4
d 3
b 2
a 1

I am completely unsure of how to do it, to i compare each number to the one before and/or after it? wouldnt that be rather slow? isnt there a faster accurate way to do it?

Any help would be greatly appericated
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Peter,

I don't quite understand your problem. Are you saying that you've a Reverse class that has an instance variable which is a 2D array? The first column holding String values & the second columns holding Integer object values?

Or the 2D array is not contained in the object?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Hey yo,

It is a 1d array of a class Object (code below);



I am trying to sort the array by the Value(d) in decesinding order. Sorry if I am not using correct terms, I have gown very acustom to map's and lists.
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Ok i have been checking out the java API and have found this;

sort(Object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements

is there a way I can use this method in my case? if so how?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You should look at the Collections framework.
run the program:
java Reverse 9 8 7 6 5 4 3 2 1

the answer being
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Implement the Comparable interface:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

Cheers, Neil
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Also have you considered using one of the classes that implement the Map interface which are designed to hold key/value collections?

Learn the default librarys and you will find that alot of work has been already done by the experts, tested and used by the millions of java programmers worldwide. Don't reinvent the wheel !
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Hey yo,

Now i am getting some really weird errors when I try and call the Arrays.sort(rev) method,
including a NullPointerException




What am i doing wrong? the error is occuring on the line of the Arrays.sort(rev)
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
I am so lost I fell like my head is going to explode, This is a really complicated program, I aplogize to the admin's but I fell like I will need to post all of it to make any sense what-so-ever;



I am using this class as an array to store infomation about the trips of the vehicles.



This is the class that is suppose to store the array to be sorted and reversed (using it as an array)



That is the main class that handles everything, input, output and error handling.



The class that will probbaly cause me to loose 10 years of my life, this is the problem class (i think) it handles the maps, arrays and how to create and store them.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Peter,

Neil had already given you the solution to your woes. Take a look at it.

Cheers!
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Hey yo,

I relize that and I am very thankfull, but I get errors when trying to do it that way. e.g I cannot use the Arrays.sort() method
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Sorry I will be more clear;

I am using the idea of a class that extends the Comparison class, I am using this as an array, I am attempting to sort it from another class, I am getting a NullPointerException on the following method;



Which is located in the reverse class, you have helped me out beyond what I could have hoped for guy's, if I can just get past the last road-block I will be extermly greatful and happy for your help.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Your above method has a possible source for the NullPointerException that you get. What happens if the parameter o is not actually an object of type Reverse, for some reason? The cast (Reverse)o will return null and attempting to call a method on this null reference will cause a NullPointerException! Of course, if this happens, there's probably a problem somewhere else in your program. Perhaps you can do something like

I'm not saying this will fix all of your problems. However, it will eliminate this particular source of an error, and even alert you later if you modify something that causes this.

HTH

Layne


Java API Documentation
The Java Tutorial
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
My apologise, I didn't read the posts carefully enough.

My suggestion would be to use Collections.sort(List l, Comparator c). If you're using an Object[] to store your Reverse objects, you need to know the size before hand. But using List, you can have dynamic sizes.

Then by implementing the Comparator class, you can specify the sort order you need.

HTH.
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
You have created an array of Reverse items using:

This array is initialised with 100 null Reverse references.
You cannot use Arrays.sort when part of this array contains nulls.
Better to use a dynamically growing list, and use Collections.sort:

This should hopefully solve your problems.
Cheers, Neil
 
GeeCON Prague 2014
 
subject: Sorting array's