Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript按对象值排序,该对象值具有多个含义(组),仅具有比较符_Javascript_Sorting - Fatal编程技术网

Javascript按对象值排序,该对象值具有多个含义(组),仅具有比较符

Javascript按对象值排序,该对象值具有多个含义(组),仅具有比较符,javascript,sorting,Javascript,Sorting,我有一些数据格式如下: var data = [ { id: 1, name: "Netherlands", population: 17}, { id: 1.1, name: "Rotterdam", population: 4}, { id: 1.2, name: "Amsterdam", population: 2}, { id: 2, name: "USA", population: 350}, { id

我有一些数据格式如下:

var data = [
        { id: 1, name:  "Netherlands", population: 17},
        { id: 1.1, name: "Rotterdam", population: 4},
        { id: 1.2, name: "Amsterdam", population: 2},
        { id: 2, name:  "USA", population: 350},
        { id: 3, name: "Germany", population: 55},
        { id: 3.1, name: "Berlin", population: 4},
        { id: 3.2, name: "Stuttgard", population: 3},
        { id: 3.3, name: "Cologne", population: 3},
        { id: 4, name: "UK", population: 60},
        { id: 5, name: "Canada", population: 30},
        { id: 5.1, name: "Ottawa", population: 2},
    ];
基本上,名称可以是城市或国家。国家以整数表示,城市以整数表示。每个国家可以有0-3个城市

是否可以为Array.prototype.sort编写一个比较器函数,例如按字母顺序排序的国家,然后按其城市排序,但保持国家/城市分组不变,并且不会事先改变数据数组?这可能吗?若否,原因为何?如果这是不可能的,我仍然有兴趣知道如何做到这一点突变阵列之前,虽然这将需要更多的回溯为我,并不是理想的

因此,按名称的字母顺序排序将得到:

var data = [
    { id: 5, name: "Canada", population: 30},
    { id: 5.1, name: "Ottawa", population: 2},
    { id: 3, name: "Germany", population: 55},
    { id: 3.1, name: "Berlin", population: 4},
    { id: 3.3, name: "Cologne", population: 3},
    { id: 3.2, name: "Stuttgard", population: 3},
    { id: 1, name:  "Netherlands", population: 17},
    { id: 1.2, name: "Amsterdam", population: 2},
    { id: 1.1, name: "Rotterdam", population: 4},
    { id: 4, name: "UK", population: 60},
    { id: 2, name:  "USA", population: 350},
];
这是一个plunker,我已经走了多远:

我不明白array.prototype.sort为什么/如何选择要比较的值,所以我不确定应该选择哪个方向。任何帮助都将不胜感激

我不明白为什么/如何
Array.prototype.sort
选择要比较的值

它根据内部使用的排序算法,从数组中任意选取它们。您的比较函数需要处理所有值

是否可以为
Array.prototype.sort
编写一个比较器函数,例如按字母顺序对国家进行排序,然后按其城市进行排序,但保持国家/城市分组不变,并且事先不改变数据数组

不,这是不可能的。要比较两个项目,您始终需要首先比较它们的国家,但如果项目是城市,则无法进行比较:城市与国家名称之间没有链接。
您需要先找到一种方法来查找它(从技术上讲,考虑到
id
,这是可能的,但是您必须事先构造一个查找表,或者在排序过程中低效地搜索数组)

您可以通过将数据更改为以下结构来帮助自己:

var data = [
    {id: 1, name:  "Netherlands", population: 17, cities: [
        {id: 1.1, name: "Rotterdam", population: 4},
        {id: 1.2, name: "Amsterdam", population: 2}
    ]},
    {id: 2, name: "USA", population: 350, cities: []},
    {id: 3, name: "Germany", population: 55, cities: [
        {id: 3.1, name: "Berlin", population: 4},
        {id: 3.2, name: "Stuttgard", population: 3},
        {id: 3.3, name: "Cologne", population: 3}
    ]},
    {id: 4, name: "UK", population: 60, cities: []},
    {id: 5, name: "Canada", population: 30, cities: [
        {id: 5.1, name: "Ottawa", population: 2}
    ]}
];
然后简单地用标准方法对国家和城市进行分类

我不明白为什么/如何
Array.prototype.sort
选择要比较的值

它根据内部使用的排序算法,从数组中任意选取它们。您的比较函数需要处理所有值

是否可以为
Array.prototype.sort
编写一个比较器函数,例如按字母顺序对国家进行排序,然后按其城市进行排序,但保持国家/城市分组不变,并且事先不改变数据数组

不,这是不可能的。要比较两个项目,您始终需要首先比较它们的国家,但如果项目是城市,则无法进行比较:城市与国家名称之间没有链接。
您需要先找到一种方法来查找它(从技术上讲,考虑到
id
,这是可能的,但是您必须事先构造一个查找表,或者在排序过程中低效地搜索数组)

您可以通过将数据更改为以下结构来帮助自己:

var data = [
    {id: 1, name:  "Netherlands", population: 17, cities: [
        {id: 1.1, name: "Rotterdam", population: 4},
        {id: 1.2, name: "Amsterdam", population: 2}
    ]},
    {id: 2, name: "USA", population: 350, cities: []},
    {id: 3, name: "Germany", population: 55, cities: [
        {id: 3.1, name: "Berlin", population: 4},
        {id: 3.2, name: "Stuttgard", population: 3},
        {id: 3.3, name: "Cologne", population: 3}
    ]},
    {id: 4, name: "UK", population: 60, cities: []},
    {id: 5, name: "Canada", population: 30, cities: [
        {id: 5.1, name: "Ottawa", population: 2}
    ]}
];

然后只需使用标准方法对国家和其中的城市进行排序。

Array.prototype.sort
选择需要比较的任何值,因此您必须准备返回排序,例如3.1与1.2,这需要查找国家名称

function compare(a, b) {
  const countryAName = array.find(elt => elt.id === countryA).name;
  const countryBName = array.find(elt => elt.id === countryB).name;

  return countryAName < countryBName ? -1 :
    countryAName > countryBName ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;
}
然后,您可以将比较器写入:

function compare(a, b) {
  return a.country < b.country ? -1 :
    a.country > b.country ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;
}
然后将比较器写为

function compare(a, b) {
  const countryAName = map[Math.floor(a.id)];
  const countryBName = map[Math.floor(b.id)];

 return countryAName < countryBName ? -1 :
    countryAName > countryBName ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;

} 
功能比较(a、b){
const countryAName=地图[数学楼层(a.id)];
const countryBName=地图[数学楼层(b.id)];
return countryNamecountryBName?+1:
a、 名称b.名称?+1:0;
} 

Array.prototype.sort选择需要比较的任何值,因此您必须准备返回订单,例如3.1和1.2,这将需要查找国家名称

function compare(a, b) {
  const countryAName = array.find(elt => elt.id === countryA).name;
  const countryBName = array.find(elt => elt.id === countryB).name;

  return countryAName < countryBName ? -1 :
    countryAName > countryBName ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;
}
然后,您可以将比较器写入:

function compare(a, b) {
  return a.country < b.country ? -1 :
    a.country > b.country ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;
}
然后将比较器写为

function compare(a, b) {
  const countryAName = map[Math.floor(a.id)];
  const countryBName = map[Math.floor(b.id)];

 return countryAName < countryBName ? -1 :
    countryAName > countryBName ? +1 :
    a.name < b.name ? -1 :
    a.name > b.name ? +1 : 0;

} 
功能比较(a、b){
const countryAName=地图[数学楼层(a.id)];
const countryBName=地图[数学楼层(b.id)];
return countryNamecountryBName?+1:
a、 名称b.名称?+1:0;
} 

您可以对country name对象使用哈希表,并对向上移动country进行一些计算

var data=[{id:1,姓名:“荷兰”,人口:17},{id:1.1,姓名:“鹿特丹”,人口:4},{id:1.2,姓名:“阿姆斯特丹”,人口:2},{id:2,姓名:“美国”,人口:350},{id:3,姓名:“德国”,人口:55},{id:3.1,姓名:“柏林”,人口:4},{id:3.2,姓名:“斯图加特”,人口:3},{id:3.3,姓名:“科隆”,人口:3},{id:4,姓名:“英国”,人口:60},{id:5,姓名:“加拿大”,人口:30},{id:5.1,姓名:“渥太华”,人口:2},],
散列={};
data.forEach(函数(a){
如果(a.id==数学楼层(a.id)){
hash[a.id]=a.name;
}
});
数据排序(函数(a,b){
函数getHash(o){返回hash[Math.floor(o.id)];}
返回(
getHash(a).localeCompare(getHash(b))| |//比较国家/地区
(Math.floor(a.id)!==a.id)-(Math.floor(b.id)!==b.id)| |//将int移到顶部
a、 name.localeCompare(b.name)//比较城市
);
});
console.log(数据);

.as console wrapper{max height:100%!important;top:0;}
您可以使用哈希表作为国家名称对象,并使用一些计算将国家向上移动

var data=[{id:1,姓名:“荷兰”,人口:17},{id:1.1,姓名:“鹿特丹”,人口:4},{id:1.2,姓名:“阿姆斯特丹”,人口:2},{id:2,姓名:“美国”,人口:350},{id:3,姓名:“德国”,人口:55},{id:3.1,姓名