• 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

NavigableSet Descending Order Haze

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:

This code from the JavaBeat SCJP1.6 bank of questions focusing on NavigableSet:

The result of this run is "3" and I understand this result is because by the time we reach line "sysprint 2", "3" will be the last entry in the ranks TreeSet. What is confounding me is the sorting of the elements in the TreeSet. I am told that the compareTo() method in the Rank class "tries to sort the elements in descending order". Well, what displays after the execution of the "sysprint 1" line of code is:

  • 4
    5
    1
    3
    2

  • Quite possibly the answer to my question is staring me in the face and I just don't see it. Can anybody please explain to me why, given the configured compareTo() method in the Rank class, the order of the elements in the ranks treeset is not in true descending order? Why am I wrong when I am expecting the elements in the TreeSet to look like:

  • 5
    4
    3
    2
    1

  • ??
    Thank you for your time.
    Gary
     
    author & internet detective
    Posts: 41860
    908
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The compare to method returns -1 that the first is smaller than the second. So if you say rank1.compareTo(rank2), it says rank1 is smaller. But if you say rank2.compareTo(rank1), it says rank2 is smaller. This is not a valid sort and clearly not descending order.

    This is why the order is tied to the order added. The actual values added don't matter.
     
    Ranch Hand
    Posts: 451
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think:

    will sort the tree in descending order.
    This means second rank is larger than the current rank, compareTo returns a positive result. The sorting algorithm will swap these two ranks in decending order if the result is positive.
    If second rank is smaller than the current rank, compareTo returns a negative result. The sorting algorithm won't swap them.
    This will create a descending sorted order.
     
    Attractive, successful people love this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic