简体   繁体   English

返回排序数组而不修改原始数组

[英]Return Sorted Array Without Modifying Original Array

I'm having trouble with a function returning the original array as opposed to the sorted array. 我遇到一个函数返回原始数组而不是排序数组。 I tried to slice the array and return the sorted but it is not working. 我试图切片数组并返回已排序,但它不起作用。 Any ideas on how to fix this? 有想法该怎么解决这个吗?

function sortArr( comparator, array ){

var newArray = array.slice();

for(var i = 0; i < newArray.size; i++)
{
    var min = i;
    for(var x = i; x < newArray.size; x++)
    {
        if(comparator(newArray[min],newArray[x]) == true)
        {
            min = x;
        }
    }

    var temp = newArray[i];
    newArray[i] = newArray[min];
    newArray[min] = temp;

}

return newArray;

}

I fixed the function: 我修复了这个功能:

function sortArr( comparator, array ){
    /*your code here*/
    var i, x;
    var min;
    var newArray = array.slice();

    for(i = 0; i < newArray.length - 1; i++)
    {
        min = i;
        for(x = i + 1; x < newArray.length; x++)
        {
            if(comparator(newArray[min],newArray[x]) == true)
            {
                min = x;
            }
        }

        if(min != i){
            var temp = newArray[i];
            newArray[i] = newArray[min];
            newArray[min] = temp;
        }

    }

return newArray;

}

Copy the array with slice and then use native sort : 使用slice复制数组,然后使用本机sort

function sortArr(comparator, array) {
  return array.slice().sort(function(a,b) {
    return comparator(a,b) * 2 - 1;
  });
}

Your sorting algorithm doesn't look quite right. 你的排序算法看起来不太合适。 For a start the swapping of values should be inside the if statement. 首先,值的交换应该在if语句中。 I would also advise to look at @Oriol's solution which is far more elegant. 我还建议看看@Oriol的解决方案,它更加优雅。

function sortArr( comparator, array ){

var newArray = array.slice();

for(var i = 0; i < newArray.size; i++)
{
    var min = i;
    for(var x = i; x < newArray.size; x++)
    {
        if(comparator(newArray[min],newArray[x]) == true)
        {                   
            var temp = newArray[i];
            newArray[i] = newArray[min];
            newArray[min] = temp;
            min = x;
        }
    }
}

return newArray;

}
{"index.js":"var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  let newArr = globalArray.slice();\n  let  emptyArr = [];
  return emptyArr.concat(newArr).sort();
}
nonMutatingSort(globalArray);"}

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

相关问题 Array.prototype没有修改原始数组 - Array.prototype without modifying original array 区分数组(不修改原始数组) - Diff an Array (without modifying original arrays) 修改复制的数组而不修改原始数组js - Modify copied array without modifying the original array js 复制对象数组并在不修改原始数组的情况下进行更改 - Copy array of objects and make changes without modifying original array 数组Slice()修改原始数组 - Array Slice() modifying original array 获取最近的上一个日期,在不修改原始数组的情况下进行排序 - Getting nearest previous date, sort without modifying original array 返回排序后的数组 React - return the sorted array React 从排序数组中删除重复项并返回长度 - 必须改变原始数组 - Remove duplicates from sorted array and return length - Must mutate the original array Javascript:排序数组并返回一个索引数组,指示排序元素相对于原始元素的 position - Javascript: Sort array and return an array of indices that indicates the position of the sorted elements with respect to the original elements 如何在不改变原始数组的情况下从时间复杂度为 O(n) 或更好的排序数组中获取唯一值 - How to get unique values from a sorted array with time complexity of O(n) or better without altering the original array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM