JavaScript-如果存在重复项,则使用另一个索引再次对数组排序

JavaScript-如果存在重复项,则使用另一个索引再次对数组排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个这样的数组: var arrValues1 = [ [11, 58], [18, 45], [13, 23], [15, 68], [23, 32], [45, 45], [19, 68], [88, 68] ]; 为了按索引1对其进行排序,我使用了以下函数: sortIn(arr, prop) { return arr.sort((a, b) => { if (a[prop] >

我有一个这样的数组:

var arrValues1 = [
    [11, 58],
    [18, 45],
    [13, 23],
    [15, 68],
    [23, 32],
    [45, 45],
    [19, 68],
    [88, 68]
];
为了按索引1对其进行排序,我使用了以下函数:

  sortIn(arr, prop) {
    return arr.sort((a, b) => {
        if (a[prop] > b[prop]) {
            return 1;
        } else if (a[prop] < b[prop]) {
            return -1;
        } else {
            return 0;
        }
    });
  } 

arrValues2.push(sortIn(arrValues1, 1));
我的问题是关于重复值(本例中为45和68)。如果我有重复的值,我需要考虑索引0值对它们进行排序。因此,最终结果将是:

var arrValues2 = [
  [13, 23],
  [23, 32],
  [18, 45],// > these 2 cases were reordered 
  [45, 45],//
  [11, 58],
  [15, 68],// > these 3 cases were reordered 
  [19, 68],//
  [88, 68] //
];

保持所有位置很重要,只更改重复值的顺序。如何实现这一点?

在值相等后,您可以继续排序…:

const arrValues2=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
函数排序值(a、b){
如果(a[1]>b[1]){
返回1;
}如果(a[1]b[0]){
返回1;
}else如果(a[0]console.log(arrValues2.sort(sort\u vals))
值相等后,可以继续排序…:

const arrValues2=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
函数排序值(a、b){
如果(a[1]>b[1]){
返回1;
}如果(a[1]b[0]){
返回1;
}else如果(a[0]console.log(arrValues2.sort(sort\u vals))
您可以将这两个键作为参数传递给自定义函数。然后使用第二个属性进行排序,如果它们相等,则在排序回调函数中检查第一个属性:

  sortIn(arr, prop) {
    return arr.sort((a, b) => {
        if (a[prop] > b[prop]) {
            return 1;
        } else if (a[prop] < b[prop]) {
            return -1;
        } else {
            return 0;
        }
    });
  } 

arrValues2.push(sortIn(arrValues1, 1));
var arrValues1=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
常数排序=(arr,prop1,prop2)=>{
返回arr.sort((a,b)=>a[prop1]-b[prop1]==0?
a[prop2]-b[prop2]:
a[prop1]-b[prop1]);
}

log(sortIn(arrValues1,1,0))
您可以将这两个键作为参数传递给自定义函数。然后使用第二个属性进行排序,如果它们相等,则在排序回调函数中检查第一个属性:

  sortIn(arr, prop) {
    return arr.sort((a, b) => {
        if (a[prop] > b[prop]) {
            return 1;
        } else if (a[prop] < b[prop]) {
            return -1;
        } else {
            return 0;
        }
    });
  } 

arrValues2.push(sortIn(arrValues1, 1));
var arrValues1=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
常数排序=(arr,prop1,prop2)=>{
返回arr.sort((a,b)=>a[prop1]-b[prop1]==0?
a[prop2]-b[prop2]:
a[prop1]-b[prop1]);
}

log(sortIn(arrValues1,1,0))
您可以为函数添加任意数量的回退排序属性。因此,您将尝试按每个排序,并且仅当它们都匹配时,才返回
0
,以表示相等:

var值=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
功能排序(arr,…道具){
//^^^^^^^^^^获取任意数量的属性进行排序
返回arr.sort((a,b)=>{
//尝试按每个进行排序。如果未找到“return”
//比较将继续到下一个版本
用于(道具的常数道具){
如果(a[prop]>b[prop]){
返回1;
}否则,如果(a[prop]JSON.stringify(x));
console.log(显示结果)

.as console wrapper{max height:100%!important;top:0}
您可以为函数添加任意数量的回退排序属性。因此,您将尝试按每个排序,并且仅当它们都匹配时,才返回
0
,以表示相等:

var值=[
[11, 58],
[18, 45],
[13, 23],
[15, 68],
[23, 32],
[45, 45],
[19, 68],
[88, 68]
];
功能排序(arr,…道具){
//^^^^^^^^^^获取任意数量的属性进行排序
返回arr.sort((a,b)=>{
//尝试按每个进行排序。如果未找到“return”
//比较将继续到下一个版本
用于(道具的常数道具){
如果(a[prop]>b[prop]){
返回1;
}否则,如果(a[prop]JSON.stringify(x));
console.log(显示结果)

.as console wrapper{max height:100%!important;top:0}
您可以通过在else块中添加更多逻辑来排序,如下所示:

sortIn(arr, prop) {
  return arr.sort((a, b) => {
    if (a[prop] > b[prop]) {
        return 1;
    } else if (a[prop] < b[prop]) {
        return -1;
    } else {
        if (a[0] > b[0]) {
           return 1
        } else {
          return -1
        }
    }
  });
}

arrValues2.push(sortIn(arrValues1, 1));
sortIn(arr,prop){
返回arr.sort((a,b)=>{
如果(a[prop]>b[prop]){
返回1;
}否则,如果(a[prop]b[0]){
返回1
}否则{
返回-1
}
}
});
}
arrValues2.推送(sortIn(arrValues1,1));

您可以通过在else块中添加更多逻辑来对其进行排序,如下所示:

sortIn(arr, prop) {
  return arr.sort((a, b) => {
    if (a[prop] > b[prop]) {
        return 1;
    } else if (a[prop] < b[prop]) {
        return -1;
    } else {
        if (a[0] > b[0]) {
           return 1
        } else {
          return -1
        }
    }
  });
}

arrValues2.push(sortIn(arrValues1, 1));
sortIn(arr,prop){
返回arr.sort((a,b)=>{
如果(a[prop]>b[prop]){
返回1;
}否则,如果(a[prop]b[0]){
返回1
}否则{
返回-1
}
}
});
}
arrValues2.推送(sortIn(arrValues1,1));

如果出现类似的情况,您可以再次进行比较

    sortIn(arr, prop) {
    return arr.sort((a, b) => {
        if (a[prop] > b[prop]) {
            return 1;
        } else if (a[prop] < b[prop]) {
            return -1;
        } else {
            // Compare Again
            // return 0;
           if (a[0] > b[0]) {
            return 1;
        } else if (a[0] < b[0]) {
            return -1;
        } else {
            return 0;
         }
        }
    });
  } 

arrValues2.push(sortIn(arrValues1, 1));
sortIn(arr,prop){
返回arr.sort((a,b)=>{
如果(a[prop]>b[prop]){
返回1;
}否则,如果(a[prop]b[0]){
返回1;
}else如果(a[0]