• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting array's

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implement the Comparable interface:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

Cheers, Neil
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

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

Cheers!
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic