Results 1 to 7 of 7
  1. #1
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Sorting a treemap by values?

    Ok guys so I have figured out how to do the treemap now and it sorted the entrys in alphabetical order and prints thee averages. Next I need to sort Treemap based on the values.

    I think I have to somehow pass the Treemap to an arraylist, sort the arraylist and then pass it to a hashmap and then print the hashmap but this is really confusing..

    Do I need to make a new class that implements comparable or something?

  2. #2
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Sorting a treemap by values?

    Ok abit of an update basically I type this.

    System.out.println(myMap.values()); and it prints out

    [1.0, 7.5, 2.9, 5.0]

    I need to pass myMap.values() to an Array so I can sort that array and then print it out again so the values will be in highest order first but I just can't figure out the line of code that will change map.values() into an array

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Sorting a treemap by values?

    Not an advanced topic. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Sorting a treemap by values?

    Quote Originally Posted by DarrylBurke View Post
    Not an advanced topic. Moving to New to Java.

    db
    If it isn't advanced maybe you can answer it?

  5. #5
    Java Junior is offline Member
    Join Date
    Mar 2012
    Posts
    13
    Rep Power
    0

    Default Re: Sorting a treemap by values?

    Bump... still need help!!!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Sorting a treemap by values?

    If it's only for display you could Collections.sort() that array.
    That's assuming it's a List, of course.
    Please do not ask for code as refusal often offends.

  7. #7
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default Re: Sorting a treemap by values?

    You can't. Plenty of workarounds though. Did you try anything Google suggested?

Similar Threads

  1. Help Plz !! SORTING OID VALUES !!!
    By srinboss in forum New To Java
    Replies: 1
    Last Post: 12-03-2011, 10:06 PM
  2. sorting a hash map without creating a new treemap
    By aisha05 in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 09:11 AM
  3. Sorting string Array Values
    By Zerpol in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 07:08 AM
  4. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 07:59 AM
  5. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •