Forums Register Login

Sorting array's

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
Implement the Comparable interface:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

Cheers, Neil
+Pie Number of slices to send: Send
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 !
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Peter,

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

Cheers!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1917 times.
Similar Threads
Big O
A little help defining my objects?
Formatting outputs based on values in resultset
Sorting in Java
SCJP 5.0 BETA Certification Exam
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:54:30.