7 Replies Latest reply: Jul 27, 2012 1:08 PM by DrClap RSS

    Exact Match on the top of the List

    J2EE_Life
      Hi All,

      I have a list as ArrayList<User> where User class contains some fields including firstname,lastname and userID.
      Given a search string say "searchString", I need to sort the list in a way that EXACT matches in the list ON the fields firstname,lastname or userID comes on the top and then the others. All the entries already satisfies the partial matching criteria. Just need to get the exact matches on the TOP of the List

      Example: if ArrayList<User> contains {{"hominder","kumar" ,"lpppp"},{"leethom","chuu" ,"lpppx"},{"thomas","dorothy" ,"lxoxp"},{"paul","meyer" ,"thomyg"},{"hom","jones" ,"lwppp"},{"susana","hom" ,"lpipp"}}
      where {"ajay","kumar" ,"lpppp"} denotes firstname,lastname and userID of User respectively.

      In this case if searchString = "hom", the I should get {"hom","jones" ,"lwppp"} and {"susana","hom" ,"lpipp"}} on the top of the list as they are exact matches. Kindly let me know a TIME Efficient way to do this because traffic of my site is much and most of them are managers. Any help is much appreciated.
        • 1. Re: Exact Match on the top of the List
          DrClap
          Search the list first for exact matches, and then search it again for approximate matches.
          • 2. Re: Exact Match on the top of the List
            J2EE_Life
            Thnks DrClap,

            The list already contains all those objects which are partial matches. i.e All elements in list are partial matches , I just needed a way for EXACT matches to be at the top of the list in a TIME- EFFICIENT way.... Can you plz suggest some way to achieve this?
            • 3. Re: Exact Match on the top of the List
              DrClap
              I thought I just did. Evidently I didn't understand your requirements correctly.

              So let's try again. Search the list for exact matches and, when you find one, move it to the top of the list.
              • 4. Re: Exact Match on the top of the List
                J2EE_Life
                Ya DrClaps But How Do I search for EXACT matches and then set it at the top of the list in a TIME- EFFICIENT way?
                • 5. Re: Exact Match on the top of the List
                  EJP
                  Just remove it and then insert it at the front. A LinkedList would be much more efficient at this.
                  • 6. Re: Exact Match on the top of the List
                    gimbal2
                    J2EE_Life wrote:
                    and then set it at the top of the list in a TIME- EFFICIENT way?
                    Your need for speed smells like premature optimization. First make it work, if then you notice it doesn't perform so well look for ways to optimize it. At this point it is apparently already difficult enough for you to figure out how to make it work.
                    • 7. Re: Exact Match on the top of the List
                      DrClap
                      gimbal2 wrote:
                      Your need for speed smells like premature optimization. First make it work, if then you notice it doesn't perform so well look for ways to optimize it. At this point it is apparently already difficult enough for you to figure out how to make it work.
                      No, I think it's analysis paralysis. It's easy enough to figure out several different solutions, but if you always have in the back of your mind "OMG what if it isn't fast!!!11!" then you're going to be distracted. Especially since the speed requirements are totally undocumented.

                      And yes, the speed requirement is a total red herring. If it's a web application then you aren't going to have hundreds of thousands of items to sort, and all of the obvious algorithms are O(N) anyway. It's just nonsense to waste even a second worrying about the speed of the processing.