如何根据匹配的字母表顺序排序字符串列表?

I am not sure if anyone else have faced this issue. I have an list of array elements which need to display on auto suggest. I biggest issue is I need to display all the categories based on matching string inside the category name.

For example - If I type auto in search box it should display matching records on auto suggest like -

Indus -> Automation -> Contribution
Construction -> Automation -> Industry
Indus -> Esautomation -> TNB
Cal -> Theadauto->Textile

It should be order based on the matching alphabets. If first 3 chars matches from category it should display that category first then second occurrence and last occurrence so on. I am using regx in jquery to display matching string. I will prefer solution either in jquery else php.

It will be really helpful if I get some solution.

I use this bound to keypress:

jQuery('select.myClass').find('option').hide();
jQuery('select.myClass').find('option:hidden[value*="' + searchFor + '"]').show();

Probably not the most efficient code in the world, but I run it against a bunch of selects, each containing upto several hundred options and not yet had a major performance issue. The same will work for anything else since all it's doing is using jQuery's *= selector.

Here for *= selector or similarly here for :contains selector

All you then need to do to achieve the correct ordering in jQuery is to pickup one of the many jQuery sort plugins (or use native javascript sort function) and sort with a function like this one:

var searchFor = 'foo';
function sortByPos(val1, val2)
{
  return (val1.indexOf(searchFor) > val2.indexOf(searchFor) ? +1 : -1);
}