在另一个键上调整对象数组而不会干扰JavaScript中的先前排序(部分排序)

我有一个这样的对象数组:

var booksArray = [  
    {Id:1,Rate:5,Price:200,Name:"History"},
    {Id:2,Rate:5,Price:150,Name:"Geographic"},
    {Id:3,Rate:1,Price:75,Name:"Maths"},
    {Id:4,Rate:2,Price:50,Name:"Statistics"},
    {Id:5,Rate:3,Price:120,Name:"Drawing"},
    {Id:6,Rate:2,Price:100,Name:"Arts"},
    {Id:7,Rate:3,Price:110,Name:"Chemistry"},
    {Id:8,Rate:4,Price:160,Name:"Biology"},
    {Id:9,Rate:4,Price:90,Name:"Software Engineering"},
    {Id:10,Rate:4,Price:80,Name:"Algorithm"},
    {Id:11,Rate:1,Price:85,Name:"Urdu"},
    {Id:12,Rate:1,Price:110,Name:"International Relations"},
    {Id:13,Rate:1,Price:120,Name:"Physics"},
    {Id:14,Rate:3,Price:230,Name:"Electronics"},
    {Id:15,Rate:3,Price:250,Name:"Jihad"},
    {Id:16,Rate:3,Price:200,Name:"Functional Programming"},
    {Id:17,Rate:2,Price:190,Name:"Computer Science"},
    {Id:18,Rate:2,Price:50,Name:"Problem solving Techniques"},
    {Id:19,Rate:6,Price:150,Name:"C#"},
    {Id:20,Rate:7,Price:250,Name:"ASP.Net"}
]

我正在用这个函数对它进行排序:

function sortBy(key, reverse) {
    var moveSmaller = reverse ? 1 : -1;
    var moveLarger = reverse ? -1 : 1;
    return function(a, b) {
        if (a[key] < b[key]) {
            return moveSmaller;
        }

        if (a[key] > b[key]) {
            return moveLarger;
        }
        return 0;
    };
} 

booksArray.sort(sortBy('Rate', false))
console.log(JSON.stringify(booksArray))

这产生了这个结果:

[
    {Id:3,Rate:1,Price:75,Name:"Maths"},
    {Id:11,Rate:1,Price:85,Name:"Urdu"},
    {Id:12,Rate:1,Price:110,Name:"International Relations"},
    {Id:13,Rate:1,Price:120,Name:"Physics"},
    {Id:4,Rate:2,Price:50,Name:"Statistics"},
    {Id:6,Rate:2,Price:100,Name:"Arts"},
    {Id:17,Rate:2,Price:190,Name:"Computer Science"},
    {Id:18,Rate:2,Price:50,Name:"Problem solving Techniques"},
    {Id:5,Rate:3,Price:120,Name:"Drawing"},
    {Id:7,Rate:3,Price:110,Name:"Chemistry"},
    {Id:14,Rate:3,Price:230,Name:"Electronics"},
    {Id:15,Rate:3,Price:250,Name:"Jihad"},
    {Id:16,Rate:3,Price:200,Name:"Functional Programming"},
    {Id:8,Rate:4,Price:160,Name:"Biology"},
    {Id:9,Rate:4,Price:90,Name:"Software Engineering"},
    {Id:10,Rate:4,Price:80,Name:"Algorithm"},
    {Id:1,Rate:5,Price:200,Name:"History"},
    {Id:2,Rate:5,Price:150,Name:"Geographic"},
    {Id:19,Rate:6,Price:150,Name:"C#"},
    {Id:20,Rate:7,Price:250,Name:"ASP.Net"}
]

你可以看到它在Rate字段上排序很好.现在我想在不干扰速率排序的情况下再次使用数组的各个部分.我需要像这样的输出:

[
    {Id:12,Rate:1,Price:110,Name:"International Relations"},
    {Id:3,Rate:1,Price:75,Name:"Maths"},
    {Id:13,Rate:1,Price:120,Name:"Physics"},
    {Id:11,Rate:1,Price:85,Name:"Urdu"},
    {Id:6,Rate:2,Price:100,Name:"Arts"},
    {Id:17,Rate:2,Price:190,Name:"Computer Science"},
    {Id:18,Rate:2,Price:50,Name:"Problem solving Techniques"},
    {Id:4,Rate:2,Price:50,Name:"Statistics"},
    {Id:7,Rate:3,Price:110,Name:"Chemistry"},
    {Id:5,Rate:3,Price:120,Name:"Drawing"},
    {Id:14,Rate:3,Price:230,Name:"Electronics"},
    {Id:16,Rate:3,Price:200,Name:"Functional Programming"},
    {Id:15,Rate:3,Price:250,Name:"Jihad"},
    {Id:10,Rate:4,Price:80,Name:"Algorithm"},
    {Id:8,Rate:4,Price:160,Name:"Biology"},
    {Id:9,Rate:4,Price:90,Name:"Software Engineering"},
    {Id:2,Rate:5,Price:150,Name:"Geographic"},
    {Id:1,Rate:5,Price:200,Name:"History"},
    {Id:19,Rate:6,Price:150,Name:"C#"},
    {Id:20,Rate:7,Price:250,Name:"ASP.Net"}
]

在这里,您可以看到它按照费率和名称排序.

为什么我不能一劳永逸地使用多个键进行排序?因为我正在创建一个对对象数组进行排序的函数,可以多次调用.

例:

myarray.order('Rate') // single sort
myarray.order('Rate').order('Name') // multiple sort

我可以在我的函数中使用更多参数来跟踪数组是否已经排序.

Sample Fiddle

最佳答案 假设您已经按照Rate排序bookArray.它可以使用以下方式在第二级进行排序.可以进行微调,以便于使用.

var arr = bookArray;
var firstSortKey = 'Rate';
var secondSortKey = 'Name';
var count = 0 ;
var firstSortValue = arr[count][firstSortKey];

for(var i=count+1; i<arr.length; i++){
     if(arr[i][firstSortKey]!==firstSortValue){
         var data = arr.slice(count, i);
         data = data.sort(function(a, b){
             return a[secondSortKey]>b[secondSortKey];
         });
         var argsArray = [count, i-count];
         argsArray.push.apply(argsArray, data);
         Array.prototype.splice.apply(arr, argsArray);
         count = i;
         firstSortValue =  arr[i][firstSortKey];
     }
}

Fiddle Demo

点赞