12 Replies Latest reply: Apr 12, 2010 4:22 PM by 796440 RSS

    Customizing the  Orde Using Comparator

    807580
      Hi all,

      Is it possible to Customize the Order by Implementing Comparator interface neither Ascending Nor Decending?
      Eg. Consider i ve Some list of Countries like Canada,Mexico and United States.
      i just want to Display a Country list
      Neither Ascending -- >
      Canada,
      Mexico and
      Unites States)

      Nor Decending -- >
      United States,
      Mexico and
      Canada).

      Just i want to Display it like in this Order -->
      United States,
      Canada and
      Mexico.

      If it is possible means How can we accomplish it by using Comparator?

      Thanks and Regards,
      Rajkumar Sundararaj
        • 1. Re: Customizing the  Orde Using Comparator
          796440
          You can define any order you want. The comparator just takes two inputs and returns negative, zero, or positive, depending on which one is "less" by your rules. So just define your rules OUTSIDE of Java to determine whether a given country is less than the others, and then translate them to Java.
          • 2. Re: Customizing the  Orde Using Comparator
            796283
            public int compareTo(Object that) {
                 if(this == USA) {
                      return Integer.MAX_VALUE; //USA numba 1!
                 } else if (that == USA) {
                      return this - Integer.MAX_VALUE;  //you no stand chance.
                 }
                 else {
                      return this - that;
                 }
            }
            I'm guessing something like that would work if you wanted one to always be at the top. But if order matters, I imagine you could use something that just preserves the ordering of when they were added, like a (non hash based) List. Linked list comes to mind if you want to still be able to sort, because you could sort ascending/descending, remove USA and add it as the head of the list (might be easier than using a comparator).
            • 3. Re: Customizing the  Orde Using Comparator
              796440
              bogdana wrote:
              } else if (that == USA) {
              return this - Integer.MAX_VALUE;  //you no stand chance.
              }
              else {
              return this - that;
              }
              }
              Aside from the obvious syntax errors, you do not want to subtract like that. You can wrap around Integer.MIN/MAX_VALUE and get the opposite sign of what you expect. Just return -1, 0, or 1.
              • 4. Re: Customizing the  Orde Using Comparator
                796283
                I was going for a more comical response (USA being the "biggest"; what, that's up to you) and give a general idea. I didn't even know what would happen if you added/subtracted beyond the bounds of an int, but that uncertainty alone would be enough for me to not use Max/Min values (or at least do some boundary testing to see what would happen).

                But personally, I'd go with the LinkedList. Maybe write a basic comparator to be able to sort ascending/descending (if collections couldn't do it for me), and just move what I wanted to the top.
                • 5. Re: Customizing the  Orde Using Comparator
                  807580
                  You can use a Comparator if you know that the data has to be ordered in a specific way (ascending or descending).. But if you don't have any specific algorithm and u need to display the data in your own way, i think its better to create a collection of type java.util.List (eg; ArrayList) instead of using a Comparator. Coz List stores data exactly in the order how you add into it.

                  In a nut shell, use a ordered collection instead of Comparator..

                  A * R
                  • 6. Re: Customizing the  Orde Using Comparator
                    796440
                    bogdana wrote:
                    I was going for a more comical response (USA being the "biggest"; what, that's up to you)
                    Yeah, I got that, but since your solution was also a decent first approximation to the real one, I wanted to point out the pitfalls for the OP's benefit.
                    I didn't even know what would happen if you added/subtracted beyond the bounds of an int, but that uncertainty alone would be enough for me to not use Max/Min values (or at least do some boundary testing to see what would happen).
                    It's not even that you explicitly use MIN/MAX in your code. Don't subtract at all. If obj1 has a value that's MAX / 2 + 1 and obj2 has a value that's MIN / 2 - 1, or any other combination where the difference is greater than Integer.MAX_VALUE, you'll get the wrong sign.
                    But personally, I'd go with the LinkedList. Maybe write a basic comparator to be able to sort ascending/descending (if collections couldn't do it for me), and just move what I wanted to the top.
                    I'd go with a Comparator that gives me my desired order. Keep sorting operations where they belong.
                    • 7. Re: Customizing the  Orde Using Comparator
                      796440
                      AnonymousGuy wrote:
                      You can use a Comparator if you know that the data has to be ordered in a specific way (ascending or descending).. But if you don't have any specific algorithm and u need to display the data in your own way, i think its better to create a collection of type java.util.List (eg; ArrayList) instead of using a Comparator. Coz List stores data exactly in the order how you add into it.

                      In a nut shell, use a ordered collection instead of Comparator..

                      A * R
                      The whole point is that he has stuff arriving in an unpredictable order, and he wants it to end up in a particular predictable order. This is exactly what you use a Comparator for.
                      • 8. Re: Customizing the  Orde Using Comparator
                        796283
                        jverd wrote:
                        It's not even that you explicitly use MIN/MAX in your code. Don't subtract at all. If obj1 has a value that's MAX / 2 + 1 and obj2 has a value that's MIN / 2 - 1, or any other combination where the difference is greater than Integer.MAX_VALUE, you'll get the wrong sign.
                        Noted. That's one aspect I hadn't even thought about, but in my cases, I could guarantee the particular values of things I wanted to compare (in the form of field validation), so I almost never would've gotten that scenario. I was curious though so I looked it up, and sure enough, Java doesn't recommend using "return this.field - that.field", even though that is how the first couple pages I went to learn how to use comparators implemented it (the Java tutorial wasn't among the top 3-4 results on google, at least with my query).
                        • 9. Re: Customizing the  Orde Using Comparator
                          796440
                          bogdana wrote:
                          jverd wrote:
                          It's not even that you explicitly use MIN/MAX in your code. Don't subtract at all. If obj1 has a value that's MAX / 2 + 1 and obj2 has a value that's MIN / 2 - 1, or any other combination where the difference is greater than Integer.MAX_VALUE, you'll get the wrong sign.
                          Noted. That's one aspect I hadn't even thought about, but in my cases, I could guarantee the particular values of things I wanted to compare (in the form of field validation), so I almost never would've gotten that scenario. I was curious though so I looked it up, and sure enough, Java doesn't recommend using "return this.field - that.field", even though that is how the first couple pages I went to learn how to use comparators implemented it (the Java tutorial wasn't among the top 3-4 results on google, at least with my query).
                          It will work fine in a lot of common cases. But it's easy enough to break, and for the bad behavior to be subtle or not show up until something's in production for a long time, that I always go with explicit -1, 0, 1, just to be in the habit and not accidentally use subtraction when it's not appropriate.
                          • 10. Re: Customizing the  Orde Using Comparator
                            807580
                            What's that predictable order that he expects?

                            Edited by: AnonymousGuy on Apr 12, 2010 2:11 PM
                            • 11. Re: Customizing the  Orde Using Comparator
                              DrClap
                              AnonymousGuy wrote:
                              What's that predictable order that he expects?
                              He doesn't say. But I assume there is one, he just didn't tell us what it is. It might even be some manager saying "I want the countries in this order, A, B, C, D, and so on". However if there isn't a predictable order then a Comparator isn't going to help (if you were trying to imply that).
                              • 12. Re: Customizing the  Orde Using Comparator
                                796440
                                AnonymousGuy wrote:
                                What's that predictable order that he expects?
                                U.S., Canada, Mexico, either always at the top in that order, or else those are the only 3.

                                Were you thinking he wanted some random order?

                                His question is definitely one of sorting, and the answer is the same as for all sorting questions of this nature: Define your Comparator such that it returns -1, 0, or 1 for obj1 being less than, equal to, or greater than obj2.