javascript javascript按字符串属性对对象数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45924821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:19:17  来源:igfitidea点击:

javascript sorting array of objects by string property

javascript

提问by user8528721

I am trying to sort an array of object by a property title. This the code snippet that I am running but it does not sort anything. The array is displayed as it is. P.S I looked at previous similar questions. This one for example heresuggests and uses the same method I am using.

我正在尝试按属性对对象数组进行排序title。这是我正在运行的代码片段,但它没有对任何内容进行排序。数组按原样显示。PS我看了以前的类似问题。例如,这里建议并使用我正在使用的相同方法。

The javascript:

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();

The html code:

html代码:

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

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

</html>

回答by Always Sunny

Have you tried like this? It is working as expected

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

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);

回答by big nol

Use the < or > operator when comparing strings in your compare function.

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

see documentation

查看文档

回答by spanky

Subtraction is for numeric operations. Use a.title.localeCompare(b.title)instead.

减法用于数字运算。使用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();

回答by Christian

you can try this code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

您可以从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;
})

回答by Nuttertools

Can you try this

你能试试这个吗

FOR DESC

用于 DESC

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

or FOR ASC

或升序

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