14 Replies Latest reply: Sep 30, 2007 7:20 PM by 807605 RSS

    Need help with comparator... not working right

    807605
      Hi guys lets see if i can explain this in the right way.
      What i have are 2 arrays of strings.
      String[] pages, and String[] rank

      Suppose i have
      pages[0] = cat; pages[1] = dog; and pages[2] = mouse.
      A hashtable holds these strings so i can retrieve their index value.

      and initial rank rank[0]/* associated with cat */ = 1; rank[1]/*dog*/ = 1; and rank[2]/*mouse*/ = 1

      After calculations the rank becomes
      rank[0] = 3/2 /* new highest rank, and page value of cat*/
      rank[1] = 1/2 /* new lowest rank and page value of dog*/
      and rank[2] = 1 /* middle rank and page value of mosue*/

      Now i have to sort my pages in order of rank. I attemtp this with

      class myComparator implements java.util.Comparator<String>
      {
      public int compare(String a, String b)
      {

      int i = hashedPages.get(a);
      int j = hashedPages.get(b);

      if ((double)rank[i] > (double)rank[j])
      return -1;
      else if (i==j)
      return 0;
      else return 1;
      }
      }

      then calling
      java.util.Arrays.sort(pages, compare);
      SO it works the first time.

      sorted pages in descending oder = cat, mouse, dog

      Then rank becomes

      rank[0] = 1 /* new middle rank, and page value of cat*/
      rank[1] = 1/2 /* new lowest rank and page value of dog*/
      and rank[2] = 3/2 /* high rank and page value of mosue*/

      sorted pages in descending oder = mouse, cat, dog

      Bear with me..... almost there...... Then rank becomes

      rank[0] = 5/4 /* new High rank, and page value of cat*/
      rank[1] = 3/4 /* new lowest rank and page value of dog*/
      and rank[2] = 1 /* high middle and page value of mosue*/

      sorted pages in descending oder = cat, mouse, dog

      NOW IS WHERE THE PROBLEM HITS

      My next rank order should be the final one, Sorting the pages after the following
      should not result in a new order of pages as you will see why.

      rank[0] = 5/4 /* This should remain the highest value since the rank[2] does not grow beyond
      only becomes equal to it*/
      rank[1] = 1/2 /* new lowest rank and page value of dog*/
      and rank[2] = 5/4 /* This should stay in the middle but somehow it becomes highest
      and rank*/

      Sorted pages in decending order = mouse, cat dog; THIS IS NOT WHAT I EXPECT
      What should happen is the rank should stay the same, returning me the pages as foloed
      pages[0] = cat
      pages[1] = mouse;
      pages[2] = dog

      Sorry if this is the must vauge and unclear question youve ever read. Its tough to explain with showing you my code and this is my only issue. Thanks for reading this, any input will help. Im hoping there is something wrong with my comparator.
        • 1. Re: Need help with comparator... not working right
          796440
          bradaleyx wrote:
          Hi guys lets see if i can explain this in the right way.
          What i have are 2 arrays of strings.
          String[] pages, and String[] rank
          This is the wrong way to do it.

          Create a class that has these values as member variables, not two parallel arrays.
          • 2. Re: Need help with comparator... not working right
            796440
            Also, when you post code, put it between [code] and [/code] tags to keep it properly formatted and readable.
            • 3. Re: Need help with comparator... not working right
              807605
              Yes i should do that. pretty new to these forums.
              Is there a way to edit it and put code tags around my code?

              So you say create a class that has these values.
              the are member variables of my google class
              public class Google {
                  
                  Hashtable<String,Integer> hashedPages;
                  String[] pages;
                  int[] nLinks;
                  private double[] rank;
                  
                  int n;
                  EntryList[] rows;
              and my comparator is an inner class in my google class. should it be outside of the google class?
              • 4. Re: Need help with comparator... not working right
                796440
                bradaleyx wrote:
                Yes i should do that. pretty new to these forums.
                Is there a way to edit it and put code tags around my code?
                Above the reply button, there's a little paper and pencil icon to go back and edit the post, EXCEPT that once someone has replied to a given post, you can no longer edit it.


                >
                So you say create a class that has these values.
                Er, no.
                public class Topic {
                  String keyword; // e.g. "cat", "dog", etc.
                  int numLinks;
                  int rank; // why make rank a double? unless it's more like a rating than a rank
                  ...
                }
                • 5. Re: Need help with comparator... not working right
                  807605
                  The rank has to be doubles. have an array of linked lists which represent rows of a matrix. Then i do matrix multiplication with the rank[] . then im suppose to use this rank to sort the pages according to the rank to get the new rank of the pages uugg so confused......
                  • 6. Re: Need help with comparator... not working right
                    796440
                    bradaleyx wrote:
                    The rank has to be doubles.
                    Okay, that's fine. To me, "rank" means "position", as in "the 5th one", so the name was confusing.

                    have an array of linked lists which represent rows of a matrix. Then i do matrix multiplication with the rank[] . then im suppose to use this rank to sort the pages according to the rank to get the new rank of the pages uugg so confused......
                    This doesn't tell me anything about what specific problem you're having.
                    • 7. Re: Need help with comparator... not working right
                      807605
                      ur right... im having trouble explaining my problem.

                      Basically what im suppose to do is matrix multiplication.
                      After each time i multiply i am suppose to store the new rank. this much i can do.
                      After i get the rank, i am suppose so use the comparator to sort the pages in descending rank order. if this sorted list of pages gets repeated twice without a change in order, i am suppose to return the array of pages.
                      If the sorted list changes from one time to the next, i am suppose to multiply my matrix by the rank array again, which will return a new rank array (which could be the same) and sort the pages again accordingly. So at some point i should get 2 identlical page arrays back to back.
                      This is not happening. for some reason sort is chaging the order even the the doesnt change.

                      i am callling java.util.Arrays.sort(pages, compare); after each matrix multiplication
                      • 8. Re: Need help with comparator... not working right
                        807605
                        ok i think i figured out my question. I am trying to sort in descending order. For some reason my comparator switches array items that are equal when i would like them to stay the same. Why is this happeneing?
                        class myComparator implements java.util.Comparator<String>
                               { 
                                   
                                   public int compare(String a, String b)
                                  {
                                      int i = hashedPages.get(a);
                                      int j = hashedPages.get(b);
                                
                                      if (rank[i] > rank[j])
                                          return -1;
                                      else if (i==j)
                                          return 0;
                                      else return 1;
                                  }
                              }
                        • 9. Re: Need help with comparator... not working right
                          807605
                          Arrays.sort() is stable - that is, it won't alter equal elements.

                          But do you mean:
                          else if (rank[i] == rank[j])
                          • 10. Re: Need help with comparator... not working right
                            807605
                            Arrays.sort is stable however it sorts in Ascending order. I need to sort in descending order wo i used this comparator and for some reason it does switch equal elements

                            im actually not exactly sure what this comparator even does. im guessing it is suppose to see if they are equal, and not switch them. if one is greater than the other it does switch them and if not then it keps it there....
                            • 11. Re: Need help with comparator... not working right
                              807605
                              i used this comparator and for some reason it does switch equal elements
                              SSCCE* please, or it didn't happen.
                              im actually not exactly sure what this comparator even does.
                              A Comparator provides a method that compares two things. It returns a positive int if the first is bigger, a negative int if the second one is, and zero if they're equal. That's all it does.

                              It is Arrays.sort() that does the sorting, based on the method that the Comparator supplies. You don't have to worry about how - because this isn't documented. (Except for the fact that the sort is stable.)

                              Did you notice the different version of the "else if(i==j)" line that I posted? It looks rather odd that you check the rank, and then go on to check the bare indices.


                              * http://mindprod.com/jgloss/sscce.html
                              • 12. Re: Need help with comparator... not working right
                                807605
                                >
                                Did you notice the different version of the "else if(i==j)" line that I posted? It looks rather odd that you check the rank, and then go on to check the bare indices.


                                Holy crap im an idiot...... ok give me a minute.... i think you just diagnosed my heart problef from a sperm sample :)
                                • 13. Re: Need help with comparator... not working right
                                  807605
                                  THANK YOU THANK YOU THANK YOU..... im an idiot

                                  I have no idea how i stared at this thing for hours and didnt notice that i was just comparing the index values and not the values at each indes... HAHAHAHAH wow....Thank a lot to all that helped.... next time i will be sure to use proper format. I had never read the SSCCE before so now i know. Thanks a lot guys

                                  Edited by: bradaleyx on Sep 30, 2007 5:43 PM
                                  • 14. Re: Need help with comparator... not working right
                                    807605
                                    No problem - I'm glad you got that sorted out.

                                    About the SSCCE business: it's a very good way of explaining a problem to others, because they can see it. Constructing one is sometimes not easy, but is an excellent problem solving technique. It involves removing everything extraneous from the problem and doing so often makes the problem go away - or rather the problem is not reproduced because its cause lay in some feature of its context. And managing to reproduce the problem involves rethinking assumptions about that context.

                                    (In your case the faulty assumption was that two things you considered equal were being reported as equal by that Comparator.)

                                    In any case, I'll ask for one if someone tells me a well documented feature of a method is not, in fact, the case!