对数组的开头进行排序

时间:2017-01-29 17:00:11

标签: javascript arrays

我想知道如何只更改数组的开头。

例如,像这样:

["z","b","a"].sort(0 to 1);

这将是结果:

["b","z","a"]

5 个答案:

答案 0 :(得分:0)

您可以这样做:

@import

答案 1 :(得分:0)

删除要排序的数组部分。对它进行排序,然后将其重新放回阵列的其余部分。

arr=["z","b","a"];
arr2=arr.splice(0,2).sort();
final=arr2.concat(arr);

console.log(final);

或其他方式

var arr = ["z","b","a"];
var temp = arr.splice(0,2).sort();
arr = temp.concat(arr);
console.log(arr);

答案 2 :(得分:0)

您可以在需要的部分使用Array#splice,在返回之前使用Array#sortArray#concat

import com.mongodb.DB;
import org.apache.jmeter.protocol.mongodb.config.MongoDBHolder;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBCollection;
import com.mongodb.*;
//======================================================================
MongoClient mongoClient = new MongoClient(new ServerAddress("${serverIP}", ${serverPort}));
DB db = mongoClient.getDB("${mongodb}");
DBCollection coll = db.getCollection("${collectionName}");

StringBuilder rs = new StringBuilder();
rs.append("Collection: \n");

BasicDBObject allQuery = new BasicDBObject();
BasicDBObject fields = new BasicDBObject();
fields.put("name", 1);     //projected fields
fields.put("age", 1);      //projected fields
fields.put("eyeColor", 1); //projected fields
fields.put("balance", 1);  //projected fields
BasicDBObject whereQuery = new BasicDBObject();
//whereQuery.put("gender", "female");
whereQuery.put("age", new BasicDBObject("$gt", 30));

DBCursor cursor = coll.find(whereQuery, fields);
while( cursor.hasNext() )  {
        DBObject obj = cursor.next();
        rs.append(obj.toString());
        rs.append("\n");
}
rs.toString();
//String variable at the LAST LINE of this Groovy script will be displayed in Results tab, even if no Display methods are invoked !!!

原地,使用相同的阵列。

function sort(array, start, end) {
    var a;
    array = array.slice();
    a = array.splice(start, end - start + 1);
   
    return a.sort().concat(array);
}

var data = ["z","b","a"];
console.log(sort(data, 0, 1));

答案 3 :(得分:0)

使用Array#splice(也从数组中删除元素)获取要排序的部分,对它们进行排序并使用Array#splice方法插入到同一位置。

function sortPart(arr, i, j) {
  // put the sorted value in the same position from where it removed
  // you can although use spread syntax(...) instead of `apply()`
  [].splice.apply(arr,
    // put the arguments in an array
    [i, 0].concat(
      // get the portion from the original array and sort it
      arr.splice(i, j - i + 1).sort()
    )
  );
  return arr;
}

var arr = ["z", "b", "a"];

function sortPart(arr, i, j) {
  [].splice.apply(arr, [i, 0].concat(arr.splice(i, j - i + 1).sort()));
  return arr;
}

sortPart(arr, 0, 1);

console.log(arr);

使用spread syntax

var arr = ["z", "b", "a"];

function sortPart(arr, i, j) {
  arr.splice(i, 0, ...arr.splice(i, j - i + 1).sort());
  return arr;
}

sortPart(arr, 0, 1);

console.log(arr);

注意:此答案适用于任何索引范围,不仅适用于数组的开头。

答案 4 :(得分:0)

如果你只想从0到n对数组的开头进行排序,我建议你只需要传递范围的长度来排序。在ES6中

var beginSort = (array, length) => array.slice(0,length).sort().concat(array.slice(length));
var input = ["z","b","a"];
console.log(beginSort(input, 2));