简体   繁体   English

如何在JavaScript或jQuery中对每个数组记录具有多个对象的数组进行排序

[英]How to sort an array in JavaScript or jQuery with multiple objects per array record

Before you may vote me down: I have checked all existing solutions around this problem but none was similar to this. 在您否决我之前:我已经检查了所有有关此问题的解决方案,但没有一个与此类似。

I want to sort this array alphabetically by image.name (JavaScript or jQuery): 我想按image.name (JavaScript或jQuery)按字母顺序对数组进行排序:

var myArray = [{
    creator: {
      "firstname": "Sally",
      familyname: "Bloomfield"
    },
    location: {
      "lat": 12.123,
      lng: 8.846
    },
    image: {
      name: "Hello World",
      size: "50x80"
    }
  },
  {
    creator: {
      firstname: "Bob",
      familyname: "Jones"
    },
    location: {
      lat: 31.593,
      lng: 96.376
    },
    image: {
      name: "Flowerpower",
      size: "40x50"
    }
  },
  {
    creator: {
      firstname: "John",
      familyname: "Walker"
    },
    location: {
      lat: 27.184,
      lng: 123.120
    },
    image: {
      name: "Guiness",
      size: "33x66"
    }
  }
];

The array output after sort: 排序后的数组输出:

[{
    creator: {
      firstname: "Bob",
      familyname: "Jones"
    },
    location: {
      lat: 31.593,
      lng: 96.376
    },
    image: {
      name: "Flowerpower",
      size: "40x50"
    }
  },
  {
    creator: {
      firstname: "John",
      familyname: "Walker"
    },
    location: {
      lat: 27.184,
      lng: 123.120
    },
    image: {
      name: "Guiness",
      size: "33x66"
    }
  },
  {
    creator: {
      firstname: "Sally",
      familyname: "Bloomfield"
    },
    location: {
      lat: 12.123,
      lng: 8.846
    },
    image: {
      name: "Hello World",
      size: "50x80"
    }
  }
]

So first image F lowerpower then G uiness then H ello World. 所以第一图像F lowerpower则G uiness则H ELLO世界。

The existing sort function of Javascript can't handle this as I have tried that out. 我尝试过的Javascript现有排序功能无法处理此问题。 Is it even possible to sort such structures? 甚至可以对这种结构进行排序吗?

Any help is appreciated. 任何帮助表示赞赏。

You can use localeCompare when sorting strings. 您可以在对字符串进行排序时使用localeCompare Then it's just one line using Array.sort() : 然后,使用Array.sort()仅一行:

 var myArray = [{creator: { "firstname": "Sally", familyname: "Bloomfield"},location: { "lat": 12.123, lng: 8.846},image: {name: "Hello World", size: "50x80"}},{creator: { firstname: "Bob", familyname: "Jones"},location: { lat: 31.593, lng: 96.376},image: {name: "Flowerpower", size: "40x50"}},{creator: { firstname: "John", familyname: "Walker"},location: { lat: 27.184, lng: 123.120},image: {name: "Guiness", size: "33x66"}}]; myArray.sort((a,b) => a.image.name.localeCompare(b.image.name)) console.log(myArray) 

myArray.sort(function(a, b) {
    if (a.image.name === b.image.name) {
        return 0;
    }
    return a.image.name < b.image.name ? -1 : 1;
});

You might be running into issues because you have a typo in your array. 您可能会遇到问题,因为阵列中有错字。 There's a lone double quote " on line 14 . 有一个孤独的双引号"line 14

This worked for me when I removed the extra quote: 当我删除多余的报价时,这对我有用:

var myArraySorted = myArray.sort((a, b) => {
   if (a.image.name < b.image.name) {
       return -1
   }
   return 1;
});

The arguments for the Array.prototype.sort() function compare items and shift their indexes alphabetical for type String and numerically for type Number . Array.prototype.sort()函数的参数比较项目,并将其索引按字母顺序Array.prototype.sort() type String并按数字type Number As you're sorting an Object you need to plug into the specific property you wish to target, like so... 在对Object排序时,您需要插入要定位的特定属性,就像这样……

function compare(a, b) {
    return a.image.name === b.image.name ? 0 :
        a.image.name < b.image.name ? -1 : 1;
}

var sorted = myArray.sort(compare);

Hope that helps :D 希望对您有帮助:D

Array.sort @ MDN Array.sort @ MDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM