97

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?

kirugan
  • 2,514
  • 2
  • 22
  • 41
  • You can find some useful answers to this topic here: **[Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/a/26759350/2247494)** – jherax Nov 05 '14 at 16:00

4 Answers4

120

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.

erickb
  • 6,193
  • 4
  • 24
  • 19
  • Now i need sorting use this cases: – kirugan Feb 17 '11 at 08:48
  • 62
    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; } – Tyler Oct 30 '12 at 07:47
  • 1
    You are right Tyler! When i saw the accepted solution i wanted to comment correction but you did it first. – onetdev Mar 12 '13 at 09:53
  • 14
    @Tyler I think you mean that a return value of 0 means x=y, not x=0. – abw333 Jul 22 '13 at 16:31
  • 5
    @abw333 Yes! That was a typo. Too bad I can't edit that comment anymore. – Tyler Jul 22 '13 at 22:47
  • You can find some useful answers to this topic here: **[Sorting objects in an array by a field value in JavaScript](http://stackoverflow.com/a/26759350/2247494)** – jherax Nov 05 '14 at 16:01
  • 2
    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. – William Hilton Jun 06 '16 at 01:16
  • 1
    This answer should really be changed or downvoted more as it has misled me multiple times when implementing custom sorts and not getting what I expect, and apparently there are a lot of other people experiencing the same problem (41 upvotes on Tyler's comment as of 6/13/18) – levininja Jun 13 '18 at 14:58
28

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;
  };
}
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
21

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);
Plakhoy
  • 1,846
  • 1
  • 18
  • 30
Kamyk.pl
  • 309
  • 2
  • 8
  • 4
    `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'));` – Bob Stein Feb 24 '18 at 00:05
  • I like the arithmetic operation. Looks tidier than explicit checks/returns. – akinuri May 04 '20 at 09:40
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;
}