Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2013
    Rep Power

    Default Natural Sorting vs Custom Sorting

    I know that TreeSet and TreeMap order the elements according to natural sorting, but do the two structures have anything to do with custom sorting?

    Quoting the SCJP book:
    TreeMap lets you define a custom sort order(via a Comparable or Comparator) when you constuct a TreeMap

    But the below link shows an example where neither treeset nor treemap is used.

    Difference between Comparator and Comparable in java | Java tutorial for beginners

    So the question is simple. Is there any relation between sorted data structures and custom sorting?

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Natural Sorting vs Custom Sorting


    TreeMap has a constructor that takes a Comparator which is then used. So you could use your custom ordering in a TreeMap if you create a Comparator.

    With kind regards,


Similar Threads

  1. Help with Sorting
    By Acaul in forum New To Java
    Replies: 2
    Last Post: 12-07-2012, 11:06 AM
  2. Sorting objects in a custom list
    By Parashurama in forum New To Java
    Replies: 17
    Last Post: 09-14-2012, 05:40 PM
  3. Sorting by Last Name
    By burrish in forum New To Java
    Replies: 4
    Last Post: 10-15-2011, 05:39 PM
  4. Sorting
    By Freakzoyd in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 02:02 AM

Posting Permissions

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