Comparator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    Comparator

    How do we compare objects in java? for example if we have a student class and we want to compare their last names and sort them
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by mia023
    How do we compare objects in java? for example if we have a student class and we want to compare their last names and sort them
    If there is a natural ordering you can have the class implement Comparable:

    Comparable API

    And in general, you can define a Comparator:

    Comparator API

    Despite their similar names, don't confuse them. For example, String is Comparable, because there is a natural ordering on Strings; the String class also provides the Comparator CASE_INISENSITI VE_ORDER to allow you to compare pairs of string, ignoring their case.

    CASE_INSENSITIV E_ORDER API

    Also, check out Sun's tutorial on this:

    Collection Algorithms

    Comment

    • mia023
      New Member
      • May 2007
      • 89

      #3
      Comparator

      How do we compare objects in java? for example if we have a student class and we want to compare their last names and sort them
      If I want to use an array to solve this problem How can I do that??

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by mia023
        How do we compare objects in java? for example if we have a student class and we want to compare their last names and sort them
        If I want to use an array to solve this problem How can I do that??
        IIRC, comparators just compare two items and return a negative number, a positive number or zero if they are equivalent.

        Since strings have a built in comparator all you need to do is implement the comparator in your student class and use the last name string data and compare it and pass up the result.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by mia023
          How do we compare objects in java? for example if we have a student class and we want to compare their last names and sort them
          If I want to use an array to solve this problem How can I do that??
          You posted this question yesterday.

          yesterday's post

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            mia023,

            Do not double post your questions. Doing so again will get you severe consequences.

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by mia023
              If I want to use an array to solve this problem How can I do that??
              One way is to use the utility class java.util.Array s:

              java.util.Array s

              Comment

              Working...