• 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

Arrays.sort() and some arrays.

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello. i am in a need of an idea on how to do this sort of a method...


i have here an int [] with 10 as length...

so its like...

this values are to be put on a separate set of int variables



i wanted to come up when i sorted it to this output...

v10 = 1
v1=2
v2=3
v8=3
v5=4
v7=5
v3=7
v6=7
v4=9
v9=9



i want if i sort out the values of the values[], it will be sorted but the variable they are originally assigned on will follow the sequence of the sorted values...

any ideas to do this?
i am lacking out of ideas that's why i asked help here.
hoping somebody could lend me a hand.

thanks in advance.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create a wrapper class for your variable-name / value pair like this:

provide hashCode() and compareTo() methods based on Integer values, then create an ArrayList of these wrapper objects
and sort this ArrayList with Collections.sort method. Finally, iterate trough the array and print name / value pairs.
You could also create an array of these objects and sort it with Arrays.sort.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ireneusz... i'll try to study hashes.

it is something new for me...
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:You could create a wrapper class for your variable-name / value pair like this:


I think that you'll also want to make this wrapper class implement Comparable<IntWrapper>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic