在javascript中,如何对数组的子集进行排序? [英] in javascript, how do you sort a subset of an array?

查看:75
本文介绍了在javascript中,如何对数组的子集进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,并希望排序除最后n个元素以外的所有元素。

I have an array and would like to sort all but the last n elements.

例如,如果数组长度为10个元素,则希望元素0到0当元素8-9留在原位时进行排序。

For example, if the array is 10 elements long, would like elements 0 through 7 to be sorted while elements 8-9 are left in place.

推荐答案

var array = [5, 2, 6, 4, 1, 9, 3, 8, 7];
array = array.slice(0, 7).sort().concat(array.slice(7, 10));
// array is now [1, 2, 3, 4, 5, 6, 9, 8, 7]

这篇关于在javascript中,如何对数组的子集进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆