Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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,array.sort()仅基于其中一个的两个数组_Javascript_Arrays_Sorting - Fatal编程技术网

JavaScript,array.sort()仅基于其中一个的两个数组

JavaScript,array.sort()仅基于其中一个的两个数组,javascript,arrays,sorting,Javascript,Arrays,Sorting,我刚刚发现了array.sort() 我一直在使用冒泡排序手动进行排序,因为数组很小,但我想知道是否可以使用array.Sort()来代替这个: // Sort rowCategories[i] by rowWidth[i] swapped = true; while (swapped) { swapped = false; for (var i = 0; i < rowCategories.length-1; i++) { if (rowWidth[i]

我刚刚发现了
array.sort()

我一直在使用冒泡排序手动进行排序,因为数组很小,但我想知道是否可以使用
array.Sort()
来代替这个:

// Sort rowCategories[i] by rowWidth[i]
swapped = true;
while (swapped) {
    swapped = false;
    for (var i = 0; i < rowCategories.length-1; i++) {
        if (rowWidth[i] < rowWidth[i+1]) {
            var swap = rowCategories[i];
            rowCategories[i] = rowCategories[i+1];
            rowCategories[i+1] = swap;
            swap = rowWidth[i];
            rowWidth[i] = rowWidth[i+1];
            rowWidth[i+1] = swap;
            swapped = true;
        }
    }
}
//按行宽[i]对行类别[i]排序
交换=真;
while(交换){
交换=假;
对于(变量i=0;i

我会为内置排序编写什么来完成等效的工作呢?

实际上,排序函数使用的方式取决于浏览器JS引擎实现,例如,我相信Mozilla使用MergeSort

默认情况下,它会将数组项作为字符串进行比较,如果需要考虑其他因素,则必须传递自己的比较函数进行排序,并且函数必须返回负数、0或正数,以指示项比较结果

你可以用这个排序函数代替你的,那会快得多

内置的
sort()
一次只能对一个数组进行排序,比较基于值,而不是索引


您所做的类似于PHP的
array\u multisort()
函数。如果使用加载库,则它包含此函数的实现。实现非常简单。

这只需要稍加修改。不是存储两个数组,而是存储一个带有两个属性的对象的数组。然后你可以做这样的事情

arr.sort(函数(a,b){返回a.rowWidth-b.rowWidth})


对象必须包含属性rowWidth和RowCategories

有一种对数组进行多重排序的方法,但我更喜欢对象数组。以下是多重排序:

function multisort(sortBy,otherArrays){
  var keys=[],i,tmpKeys;
  sortBy.sort(function(a,b){
    var ret=(a>b)?1:(a<b)?-1:0;
    // storing the return values to be used for the other arrays
    keys.push(ret);
    return ret;
  });
  for(i=0;i<otherArrays.length;i++){
    // copy the stored retun values
    tmpKeys=keys.concat([]);
    otherArrays[i].sort(function(){
      // return the saved values based on sortBy array's sort
      return tmpKeys.splice(0,1);
    });
  }
}

var arr1=[1,2,3],
arr2=[5,6,7],
reverse=["c","b","a"];
multisort(reverse,[arr1,arr2])
console.log(arr1);
console.log(arr2);
console.log(reverse);
函数多重排序(排序、其他数组){
var key=[],i,tmpKeys;
排序(函数(a,b){

var ret=(a>b)?1:(a你可以对一个对象数组进行排序…(w3school不是很好。作为参考,你可能想看一下)我同意,如果你将两个值都存储在一个对象中,那么你可以简单地使用
排序
行类别[]
是一个对象数组(我认为它是一个类),所以我应该将
行宽度[]
它的一部分?是的,只需为每个元素添加一个
宽度:
属性。是的,如果可能的话,我会使用这种方法。您将如何将它用于他正在实现的两个数组排序?他能做的是创建一个由成对的
行类别
行宽度
组成的数组,然后在函数中分别更改值y、 我猜他们都使用Mergesort或Quicksort?我猜这是一个端口,但这个函数是一些可怕的js代码。我很感激你这么做是为了回答这个问题(+1!),但我认为建议将
rowWidth[]
添加到
rowCategories[]
是一个更简单的解决方案,谢谢you@asimes我添加了按对象键排序,如果你有任何问题,请告诉我。多重排序是我刚才看到的一些代码。它不是我的,但找不到原始帖子。
function multisort(sortBy,otherArrays){
  var keys=[],i,tmpKeys;
  sortBy.sort(function(a,b){
    var ret=(a>b)?1:(a<b)?-1:0;
    // storing the return values to be used for the other arrays
    keys.push(ret);
    return ret;
  });
  for(i=0;i<otherArrays.length;i++){
    // copy the stored retun values
    tmpKeys=keys.concat([]);
    otherArrays[i].sort(function(){
      // return the saved values based on sortBy array's sort
      return tmpKeys.splice(0,1);
    });
  }
}

var arr1=[1,2,3],
arr2=[5,6,7],
reverse=["c","b","a"];
multisort(reverse,[arr1,arr2])
console.log(arr1);
console.log(arr2);
console.log(reverse);
var arr=[
  {id:1,col1:3,col2:2},
  {id:2,col1:2,col2:2},
  {id:3,col1:1,col2:1}
];

function sortBy(arr,keys){
  var i=0;
  arr.sort(function(a,b){
    var i=0;
    while(a[keys[i]]===b[keys[i]]&&i<keys.length){
      i++;
    }
    return (keys.length===i)?0:(a[keys[i]]>b[keys[i]])?1:-1;
  });
}
//sort by col2 then col1
sortBy(arr,["col2","col1"]);
console.log(arr);
//sort by id
sortBy(arr,["id"]);
console.log(arr);