最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - why array.sort not work in javascript? - Stack Overflow

programmeradmin2浏览0评论

I have array of object .I want if i add object in array it should add in sorted way .I used array to sort .but it not sort my array . here is my code

/

var arr = [{
  elem: {
    text: function() {
      return "aa";
    }
  }
}, {
  elem: {
    text: function() {
      return "yy";
    }
  }
}];
var obj = {
  elem: {
    text: function() {
      return "bb";
    }
  }
}
arr.push(obj);
arr.sort()
console.log(arr[1].elem.text())

Expected Out put

"bb"

Actual output

"yy" 

..why ? I used sort it should sort my array ?

I have array of object .I want if i add object in array it should add in sorted way .I used array to sort .but it not sort my array . here is my code

https://jsfiddle/8oczc5x5/

var arr = [{
  elem: {
    text: function() {
      return "aa";
    }
  }
}, {
  elem: {
    text: function() {
      return "yy";
    }
  }
}];
var obj = {
  elem: {
    text: function() {
      return "bb";
    }
  }
}
arr.push(obj);
arr.sort()
console.log(arr[1].elem.text())

Expected Out put

"bb"

Actual output

"yy" 

..why ? I used sort it should sort my array ?

Share Improve this question edited Apr 29, 2016 at 23:55 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked Apr 29, 2016 at 23:52 user944513user944513 12.7k52 gold badges185 silver badges348 bronze badges 6
  • Javascript can't sort array of object because, by default, the set of objects is not ordonned. – hubert Commented Apr 29, 2016 at 23:56
  • If you try with only number ( 1 , 2 , 3), it work fine. – hubert Commented Apr 29, 2016 at 23:57
  • ok so how we sort this array – user944513 Commented Apr 29, 2016 at 23:57
  • You're trying to sort the array by the results of functions called on inner children of objects in the array? You're going to need to write a custom sort function for that developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Hamms Commented Apr 29, 2016 at 23:58
  • @Hamms, is good. the syntax is arr.sort([fonctionComparaison]) – hubert Commented Apr 30, 2016 at 0:00
 |  Show 1 more ment

3 Answers 3

Reset to default 4

sort only really works "out-of-the-box" when sorting character data alphabetically. And why would you expect it to call your functions and pare them? That's really dangerous and plicated. However, you can perform your own special sort by passing it a function.

Taken from the docs (pareFunction is the function you're passing in):

If pareFunction is supplied, the array elements are sorted according to the return value of the pare function. If a and b are two elements being pared, then:

If pareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a es first.

If pareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

If pareFunction(a, b) is greater than 0, sort b to a lower index than a. pareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

arr.sort(function(a, b) {
  // localeCompare does a string parison that returns -1, 0, or 1
  return a.elem.text().localeCompare(b.elem.text());
});

You have to specify how to sort

arr.sort( (a,b) => a.elem.text().localeCompare(b.elem.text() );
function sortNumber(num1,num2) {return num1 - num2;} var numbs = [5, 17, 29, 48, 4, 21]; 
var sortnumb = numbs.sort(sortNumber);
alert(sortnumb)
发布评论

评论列表(0)

  1. 暂无评论