比sort()性能更好的原生js代码实现数组从小到大排序

Posted tt-wedos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比sort()性能更好的原生js代码实现数组从小到大排序相关的知识,希望对你有一定的参考价值。

    nums = [1,2,4,1,34,6,-1,2]
    for(let i = nums.length - 1; i > 0; i--) {
        let maxIdx = i;
        for(let j = 0; j < i; j++) {
            if(nums[maxIdx] < nums[j]) maxIdx = j;
        }
        if(maxIdx !== i) {
            let temp = nums[maxIdx];
            nums[maxIdx] = nums[i];
            nums[i] = temp;
        }
    }

 

以上是关于比sort()性能更好的原生js代码实现数组从小到大排序的主要内容,如果未能解决你的问题,请参考以下文章