对数据进行排序的简单方法是使用Array.sort() 函数。首先检查对象 2 的位置是否为假,如果是则返回 -1 以将对象 1 浮动到顶部。对对象 1 执行相反的操作。如果两者都不为假,则进行整数比较。
var data = [
{ id: 1, position: false },
{ id: 2, position: false },
{ id: 3, position: 4 },
{ id: 4, position: false },
{ id: 5, position: 2 },
{ id: 6, position: 5 },
{ id: 7, position: 3 },
{ id: 8, position: 1 }
];
data.sort(function (a, b) {
posa = a.position;
posb = b.position;
if (!posb) return -1;
if (!posa) return 1;
return posa - posb;
});
console.log(data);
但是这个解决方案可能不是最好的,因为Array.sort() 不需要根据 ECMA International 的标准:
以下摘自ECMAScript Language Specification, 2011, p.129
15.4.4.11 Array.prototype.sort(比较)
此数组的元素已排序。排序不一定是稳定的(也就是说,比较相等的元素不一定保持原来的顺序)。如果 comparefn 不是未定义的,它应该是一个接受两个参数 x 和 y 的函数,如果 x y 返回正值。
解决此问题的方法是让您设计自己的排序算法。最好是合并或快速排序算法。下面是用于按预期对数据进行排序的快速排序算法的实现。 注意:此响应底部有一个指向 JSFiddle 的链接。
/**
* Add a swap function to the Array object.
* @param a Object a.
* @param b Object b.
*/
Array.prototype.swap = function (a, b) {
var tmp = this[a];
this[a] = this[b];
this[b] = tmp;
};
/**
* A utility function to print out HTML to the screen in case the user's
* console is not enabled or present.
* @param message The message to print to the screen.
*/
var trace = function (message) {
id = 'trace';
t = $('#' + id);
if (t.length < 1) t = $('<div>', {
'id': id
}).appendTo($('body'));
t.append($('<p>', {
'html': message
}));
};
/**
* An implementation of the quick-sort algorithm that allows the user to
* use a custom comparator with the following signature
* {@code function(a, b) } which will return an integer value to determine
* ordering.
* @param array The array to be sorted.
* @param comparator The custom comparator function.
*/
var quickSort = function (array, comparator) {
var __internal_compare__ = function compare(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
comparator = comparator || __internal_compare__;
var qsort = function (array, begin, end, comparator) {
if (end - 1 > begin) {
var pivot = begin + Math.floor(Math.random() * (end - begin));
pivot = partition(array, begin, end, pivot, comparator);
qsort(array, begin, pivot, comparator);
qsort(array, pivot + 1, end, comparator);
}
};
var partition = function (array, begin, end, pivot, comparator) {
var piv = array[pivot];
array.swap(pivot, end - 1);
var store = begin;
var ix;
for (ix = begin; ix < end - 1; ++ix) {
if (piv != undefined && comparator(piv, array[ix]) < 0) {
array.swap(store, ix);
++store;
}
}
array.swap(end - 1, store);
return store;
};
qsort(array, 0, array.length, comparator);
};
// The custom compare function to be used on the array of data below
// in the quick-sort function.
var compare = function (a, b) {
var isBoolAndFalse = function (val) {
return typeof val === 'boolean' && !val;
};
if (a == b) return 0;
var posa = a.position;
var posb = b.position;
var cona = isBoolAndFalse(posb);
var conb = isBoolAndFalse(posa);
if (cona && conb) {
var ida = a.id;
var idb = b.id;
return idb - ida;
}
if (conb) return -1;
if (cona) return 1;
return posb - posa;
};
/**
* Main procedure follows:
*/
var data = [
{ id: 1, position: false },
{ id: 2, position: false },
{ id: 3, position: 4 },
{ id: 4, position: false },
{ id: 5, position: 2 },
{ id: 6, position: 5 },
{ id: 7, position: 3 },
{ id: 8, position: 1 }
];
quickSort(data, compare);
trace(JSON.stringify(data, undefined, 2));
输出:
[
{
"id": 8,
"position": 1
},
{
"id": 5,
"position": 2
},
{
"id": 7,
"position": 3
},
{
"id": 3,
"position": 4
},
{
"id": 6,
"position": 5
},
{
"id": 1,
"position": false
},
{
"id": 2,
"position": false
},
{
"id": 4,
"position": false
}
]
您可以使用以下 JSFiddle Demo 测试上述代码。我希望这能为您解决问题。