104

I use atocomplete.jquery plugin to suggest input text, as the result I get this array:

['White 023','White','White flower', 'Teatr']

When I start to search something thats begin from te substring it shows me array sorting like this:

'White','White 023','White flower', 'Teatr'

I need something like this:

 'Teatr','White','White 023','White flower'

Any ideas?

1

4 Answers 4

129

It could be that the plugin is case-sensitive. Try inputting Te instead of te. You can probably have your results setup to not be case-sensitive. This question might help.

For a custom sort function on an Array, you can use any JavaScript function and pass it as parameter to an Array's sort() method like this:

var array = ['White 023', 'White', 'White flower', 'Teatr'];

array.sort(function(x, y) {
  if (x < y) {
    return -1;
  }
  if (x > y) {
    return 1;
  }
  return 0;
});

// Teatr White White 023 White flower
document.write(array);

More Info here on Array.sort.

Sign up to request clarification or add additional context in comments.

8 Comments

Custom sort() calls are useful, but this example is misleading. The comparison function should not return a boolean, but rather a signed integer where a negative return value means x < y, positive means x > y and 0 means x = 0. For example, if x and y were integers, it would be easy to write such a function as function(x, y) { return x - y; }
You are right Tyler! When i saw the accepted solution i wanted to comment correction but you did it first.
@Tyler I think you mean that a return value of 0 means x=y, not x=0.
@abw333 Yes! That was a typo. Too bad I can't edit that comment anymore.
Agh! I just spent 10 minutes trying to figure out why my sort didn't work. Can we fix this typo? It must rank high on google for me to have found it.
|
31

For Objects try this:

function sortBy(field) {
  return function(a, b) {
    if (a[field] > b[field]) {
      return -1;
    } else if (a[field] < b[field]) {
      return 1;
    }
    return 0;
  };
}

1 Comment

Reverses the order.
24

or shorter

function sortBy(field) {
  return function(a, b) {
    return (a[field] > b[field]) - (a[field] < b[field])
  };
}

let myArray = [
    {tabid: 6237, url: 'https://reddit.com/r/znation'},
    {tabid: 8430, url: 'https://reddit.com/r/soccer'},
    {tabid: 1400, url: 'https://reddit.com/r/askreddit'},
    {tabid: 3620, url: 'https://reddit.com/r/tacobell'},
    {tabid: 5753, url: 'https://reddit.com/r/reddevils'},
]

myArray.sort(sortBy('url'));
console.log(myArray);

2 Comments

return function(a,b) { return (a[field] > b[field]) - (a[field] < b[field]); } is what I'm guessing you meant. Clever. Usage example would cinch this, e.g. my_array.sort(sortBy('my_field'));
I like the arithmetic operation. Looks tidier than explicit checks/returns.
1
function msort(arr){
    for(var i =0;i<arr.length;i++){
        for(var j= i+1;j<arr.length;j++){
            if(arr[i]>arr[j]){
                var swap = arr[i];
                arr[i] = arr[j];
                arr[j] = swap;
            }
        }
    }
return arr;
}

2 Comments

Default sort in javascript will be much faster than bubble sort. OP is asking for a custom comparator, not a custom implementation of sorting in js.
When OP explicit said he wants a custom comparator inside the sort function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.