【问题标题】:javascript sorting array of objects by string property [duplicate]javascript按字符串属性对对象数组进行排序[重复]
【发布时间】:2018-02-06 01:36:35
【问题描述】:

我正在尝试按属性title 对对象数组进行排序。这是我正在运行的代码 sn-p 但它没有对任何东西进行排序。数组按原样显示。 P.S 我查看了以前的类似问题。这个例如here 建议并使用我正在使用的相同方法。

javascript:

function sortLibrary() {
    // var library is defined, use it in your code
    // use console.log(library) to output the sorted library data
    console.log("inside sort");
    library.sort(function(a,b){return a.title - b.title;});
    console.log(library);
} 

// tail starts here
var library = [
    {
        author: 'Bill Gates',
        title: 'The Road Ahead',
        libraryID: 1254
    },
    {
        author: 'Steve Jobs',
        title: 'Walter Isaacson',
        libraryID: 4264
    },
    {
        author: 'Suzanne Collins',
        title: 'Mockingjay: The Final Book of The Hunger Games',
        libraryID: 3245
    }
];

sortLibrary();

html代码:

<html>
<head>
    <meta charset="UTF-8">
</head>

<body>
<h1> Test Page </h1>
<script src="myscript.js"> </script>
</body>

</html>

【问题讨论】:

  • “比尔盖茨” - “史蒂夫乔布斯”应该是什么?无穷大或更确切地说不是数字;)?

标签: javascript


【解决方案1】:

你试过这样吗?它按预期工作

library.sort(function(a,b) {return (a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0);} );

var library = [
    {
        author: 'Bill Gates',
        title: 'The Road Ahead',
        libraryID: 1254
    },
    {
        author: 'Steve Jobs',
        title: 'Walter Isaacson',
        libraryID: 4264
    },
    {
        author: 'Suzanne Collins',
        title: 'Mockingjay: The Final Book of The Hunger Games',
        libraryID: 3245
    }
];
console.log('before sorting...');
console.log(library);
library.sort(function(a,b) {return (a.title > b.title) ? 1 : ((b.title > a.title) ? -1 : 0);} );

console.log('after sorting...');
console.log(library);

【讨论】:

    【解决方案2】:

    减法用于数值运算。请改用a.title.localeCompare(b.title)

    function sortLibrary() {
      console.log("inside sort");
      library.sort(function(a, b) {
        return a.title.localeCompare(b.title);
      });
      console.log(library);
    }
    
    var library = [{
        author: 'Bill Gates',
        title: 'The Road Ahead',
        libraryID: 1254
      },
      {
        author: 'Steve Jobs',
        title: 'Walter Isaacson',
        libraryID: 4264
      },
      {
        author: 'Suzanne Collins',
        title: 'Mockingjay: The Final Book of The Hunger Games',
        libraryID: 3245
      }
    ];
    
    sortLibrary();

    【讨论】:

      【解决方案3】:

      在比较函数中比较字符串时使用 运算符。

      see documentation

      【讨论】:

      • 在此实例中使用 > 运算符,这在将减号运算符替换为 > 时有效。
      • @jonathan.ihm:.sort() 回调需要一个数字结果,而不是布尔值,因此需要的不仅仅是直接替换。
      • 我在控制台中运行它并确认它立即工作。另见stackoverflow.com/questions/51165/…
      • 没关系,请参阅stackoverflow.com/questions/34466125/… 以及我已更正。
      【解决方案4】:

      你可以从https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort试试这个代码

      library.sort(function(a, b){
      var tA = a.title.toUpperCase();
        var tB = b.title.toUpperCase();
        if (tA < tB) {
          return -1;
        }
        if (tA > tB) {
          return 1;
        }
        return 0;
      })
      

      【讨论】:

        【解决方案5】:

        你可以试试这个

        DESC

        library.sort(function(a,b){return a.title < b.title;});
        

        或 对于升序

        library.sort(function(a,b){return a.title > b.title;});
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-25
          • 1970-01-01
          • 2019-08-02
          • 2021-06-27
          相关资源
          最近更新 更多