Results 1 to 4 of 4

Thread: TreeMap Sorting - Java

  1. #1
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    TreeMap Sorting - Java

    Hi all!

    Is there anyway to sort a TreeMap via it's keys and values? The keys are strings and values are integers. I wish to sort via the highest value, and then the key alphabetically

    For example,

    treemap.put("test", 5);
    treemap.put("hello", 5);
    treemap.put("face", 1);
    treemap.put("lol", 8);

    How can I make the output

    lol - 8
    hello - 5
    test - 5
    face - 1

    At the moment, it just sorts the keys alphabetically.


    Hexus Trust = Odesi

  2. #2
    Registered+
    Join Date
    Dec 2009
    Posts
    21
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: TreeMap Sorting - Java

    I'm haven't worked with Java for ages - I'm more of a C++/C# guy. But as a general data structures question, I'd say a map isn't the ideal structure for this sort of thing - can you use a TreeSet and define an object that contains both the number and the string, and define the ordering that you want on that object?

    Of course, if you need to be able to look up elements by the string only this approach won't help.

  3. #3
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    Re: TreeMap Sorting - Java

    I created another tree map and defined a comparator for it, then copied all the elements from the original map into the new one. All sorted !


    Hexus Trust = Odesi

  4. #4
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: TreeMap Sorting - Java

    According to a 5 second look at the docs TreeMap has a constructor accepting a type implementing the interface Comparator that is used for ordering. Is this not what you want? Do you actually need 2 instances of TreeMap?
    To err is human. To really foul things up ... you need a computer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cannot get Java Working in IE 7.0 (Vista)
    By shuntfield in forum Help! Quick Relief From Tech Headaches
    Replies: 3
    Last Post: 07-11-2007, 10:27 PM
  2. Key Java source code to be opened
    By Steve in forum HEXUS News
    Replies: 0
    Last Post: 27-06-2005, 10:54 AM
  3. Replies: 0
    Last Post: 14-06-2005, 12:02 PM
  4. ATi + Java + BSOD?
    By Kezzer in forum PC Hardware and Components
    Replies: 1
    Last Post: 11-06-2005, 01:26 PM
  5. Java Problems
    By Applecrusher in forum Software
    Replies: 4
    Last Post: 27-04-2004, 01:12 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
  •