2

I have this fiddle which auto completes airport names.

The issue- Sorting

I found this question which addresses the issue, but I cant implement in my scenario. Not a pro!
Quick Preview from the question -

var source = ['Adam', 'Benjamin', 'Matt', 'Michael', 'Sam', 'Tim'];
$("input").autocomplete({
    source: function (request, response) {
        var term = $.ui.autocomplete.escapeRegex(request.term)
            , startsWithMatcher = new RegExp("^" + term, "i")
            , startsWith = $.grep(source, function(value) {
                return startsWithMatcher.test(value.label || value.value || value);
            })
            , containsMatcher = new RegExp(term, "i")
            , contains = $.grep(source, function (value) {
                return $.inArray(value, startsWith) < 0 &&
                    containsMatcher.test(value.label || value.value || value);
            });

        response(startsWith.concat(contains));
    }
});

I simply tried adjusting the source, but that was not working.

Also, the actual airport list contains above 35000 names, so is this search method efficient? And where and how does binary search fill in.

Community
  • 1
  • 1
arjun
  • 1,594
  • 16
  • 33
  • 1
    As long there's 35k records you might want to use MySQL queries instead – Armand Jun 02 '14 at 19:25
  • Where are you getting the airport names (what is your real source)? Usually open collections of data like this provide optional GET parameters which you can use to set the search term and sorting. So all the logic usually happens on the server-side of the source. – Alex B. Jun 02 '14 at 19:27
  • You should put this thing in a database. – Hristo Georgiev Jun 02 '14 at 19:27
  • 1
    What is "binary sorting"??? No, filtering a very long list twice is not a efficient as doing it only once. – Bergi Jun 02 '14 at 19:33
  • 1
    @Bergi, I think he was reffering to binary search. However, I have no idea why would he need that. – Alex B. Jun 02 '14 at 19:43
  • @AlexB. Got the airport data from https://github.com/jbrooksuk/JSON-Airports – arjun Jun 03 '14 at 04:28
  • @Bergi my bad. i was referring to binary search. And i presumed it would be good coz i read that it was very efficient until i stumbled upon - http://stackoverflow.com/q/17273121/2096740. So what i am doing is pattern matching. and for that and found this - http://stackoverflow.com/q/8190511/2096740. So i am still not confident which method is most preferable. – arjun Jun 03 '14 at 04:32
  • @AlexB. some of the airline/travel sites put a delay in response. even in their own site. also, there is limitation of min char limit. so like to host it locally. – arjun Jun 03 '14 at 04:34
  • @Armand i agree with you. from the little i know, db seems natural. But how inefficient wud be if i put a separate js file containing the JSON data and then searching thru that list? – arjun Jun 03 '14 at 04:37

1 Answers1

2

If you just need to sort the results alphabetically use the sort method within the response function parameters as such:

$.each(source, function(i, airportItem){
  if (airportItem.iata.toLowerCase().indexOf(searchTerm) !== -1 || airportItem.name.toLowerCase().indexOf(searchTerm) === 0)
    ret.push(airportItem.name + ' - ' + airportItem.iata);
});
response(ret.sort());

Here is a modified fiddle to illustrate: http://jsfiddle.net/xxww6/

halabuda
  • 386
  • 1
  • 11
  • some of the terms with starts with a character shud be displayed first. Your method is exciting and simple enuf for few instances. But i guess the regex method covers all spectrum. – arjun Jun 03 '14 at 05:35
  • but i believe the problem is more complicated. since there wud be multiple key, value pairs, the result shud be such dat the initially it displays exact matches and then values which is similar. What do you think? how wud, for eg, google flights wud be doing it. – arjun Jun 03 '14 at 05:53
  • More preferably, final list cud be on the basis of location. whatever matches, show the first few items based on the country of the user, and then the rest as usual. – arjun Jun 03 '14 at 06:01
  • at this point you're talking about an abstract usability issue, which is really just a matter of your preference. – halabuda Jun 03 '14 at 06:36
  • a crud method would not take this anywhere to usability which demands a refined approach. – arjun Jun 03 '14 at 07:03