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?
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?
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.
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 !
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.
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.
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.
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.
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
Post by:autobot
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards