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

javascript - Sorting of Array of Objects not working with Array.sort() method - Stack Overflow

programmeradmin0浏览0评论

I am attempting to sort an array of objects by a name property that exists on each object. When using the sort() method with the code below I am getting the following error:

ERROR ReferenceError: b is not defined

Here is my code:

myArray.sort( (a, b) => {
return (typeof a.name: string === 'string') - (typeof b.name === 'string')|| a.name - b.name || a.name.localeCompare(b.name)};

Here is what is odd though...

When I run:

myArray.sort( (a, b) => {
console.log(a.name);
console.log(b.name);

It logs the names perfectly fine. What am I missing??

Just to be a thorough little bit of context:

I am using this method after doing an HTTP call from an angular service.ts file and this array is being passed to my component and subscribed to. And I am using Angular, so this would be Typescript compiling to JavaScript. I also have another myArray.forEach() method just below my sort() method and that is working.

I am attempting to sort an array of objects by a name property that exists on each object. When using the sort() method with the code below I am getting the following error:

ERROR ReferenceError: b is not defined

Here is my code:

myArray.sort( (a, b) => {
return (typeof a.name: string === 'string') - (typeof b.name === 'string')|| a.name - b.name || a.name.localeCompare(b.name)};

Here is what is odd though...

When I run:

myArray.sort( (a, b) => {
console.log(a.name);
console.log(b.name);

It logs the names perfectly fine. What am I missing??

Just to be a thorough little bit of context:

I am using this method after doing an HTTP call from an angular service.ts file and this array is being passed to my component and subscribed to. And I am using Angular, so this would be Typescript compiling to JavaScript. I also have another myArray.forEach() method just below my sort() method and that is working.

Share Improve this question edited Oct 25, 2022 at 6:58 SMAKSS 10.5k3 gold badges24 silver badges37 bronze badges asked Mar 17, 2018 at 15:12 newmannewman 4213 gold badges9 silver badges26 bronze badges 8
  • 3 please add the array as well. – Nina Scholz Commented Mar 17, 2018 at 15:13
  • 1 The subtraction operator doesn't work on string values; what is it that you expect that expression to do? – Pointy Commented Mar 17, 2018 at 15:14
  • Doesn't look like valid typescript syntax to me. – ASDFGerte Commented Mar 17, 2018 at 15:15
  • 1 With that many || operators, you would be best to add parentheses throughout, but really, you might want to go back to using something easier to debug using good old-fashioned if .. else – vol7ron Commented Mar 17, 2018 at 15:17
  • 1 Please add a minimal, complete and verifiable example which shows the actual problem. – Andreas Commented Mar 17, 2018 at 15:20
 |  Show 3 more comments

2 Answers 2

Reset to default 17

Is this what you want?

var a = [
  { name: "John" },
  { name: "Jack" },
  { name: "Bob" }
];

a.sort(function (a, b) {
  if (a.name > b.name) return 1;
  if (a.name < b.name) return -1;
  return 0;
});

console.log(a);

You could use a comparison which works independently of the type of string or number, by moving numerical values to top.

var array = [{ name: 20 }, { name: 21 }, { name: 2 }, { name: 11 }, { name: 1 }, { name: 'John' }, { name: 'Jane' }, { name: 'Frank' }, { name: 'Mary' },] 

array.sort((a, b) => (typeof a.name === 'string') - (typeof b.name === 'string') || a.name > b.name || -(a.name < b.name));

console.log(array);

发布评论

评论列表(0)

  1. 暂无评论