我试图对一个 div 数组进行排序,这样如果 a
在 b
的下方或左侧,则 a
在 b
.
在使用 CodePen 几个小时后,我意识到如果一个数组的长度为十个或更多项,Chrome 会乱序排序,至少使用这个比较函数是这样:
var array = [0,1,2,3,4,5,6,7,8,9,10];
array.sort(function(a, b) {
return -1;
});
Chrome 返回:
[0, 2, 3, 4, 1, 6, 7, 8, 9, 10, 5]
如果您在 sort 函数中记录 a 和 b,那么它发生的原因就很明显了——这只是 Chrome 使用的算法。我知道人们使用 return a-b 等...但让我们转向以下函数...数组是包含 div 的 jQuery 对象。如果 a 在 b 的下方或左侧,我希望 a 在 b 之前出现。有帮助吗??
编辑:为了回应此处的一些答案,我重写了函数以输出 1
、-1
或 0
。尽管如此,我还是得到了不想要的结果。查看输出中第一个对象的 right
属性如何大于第二个对象的 left
属性,而第一个对象的 top
属性如何低于第二个 底部
。根据比较功能,它们应该是相反的顺序。
var array = [
{
bottom:1181.8854675292969,
left:23.39583396911621,
right:72.39583396911621,
top:910.8854675292969,
},
{
bottom:1181.3750305175781,
left:78.39583587646484,
right:183.39583587646484,
top:1132.3750305175781
},
{
bottom:1182.6042175292969,
left:189.39584350585938,
right:349.3958435058594,
top:1021.6042175292969
},
{
bottom:1181.3750305175781,
left:355.3958435058594,
right:626.3958435058594,
top:1132.3750305175781
},
{
bottom:1133.2292175292969,
left:355.3958435058594,
right:632.3958435058594,
top:1132.2292175292969
},
{
bottom:1127.0208435058594,
left:78.39583587646484,
right:183.39583587646484,
top:1022.0208435058594
},
{
bottom:1127.0208435058594,
left:355.3958435058594,
right:460.3958435058594,
top:1022.0208435058594
},
{
bottom:1127.0208435058594,
left:466.3958435058594,
right:571.3958435058594,
top:1022.0208435058594,
},
{
bottom:1016.0208435058594,
left:78.39583587646484,
right:183.39583587646484,
top:911.0208435058594
},
{
bottom:1016.2395935058594,
left:189.39584350585938,
right:515.3958435058594,
top:800.2395935058594
},
{
bottom:1016.2395935058594,
left:521.3958740234375,
right:626.3958740234375,
top:800.2395935058594
},
{
bottom:906.0208435058594,
left:23.39583396911621,
right:183.3958339691162,
top:801.0208435058594
},
{
bottom:794.6041870117188,
left:23.39583396911621,
right:72.39583396911621,
top:634.6041870117188
},
{
bottom:795.0208435058594,
left:78.39583587646484,
right:183.39583587646484,
top:690.0208435058594
},
{
bottom:794.0208435058594,
left:189.39584350585938,
right:404.3958435058594,
top:689.0208435058594
},
{
bottom:794.0208435058594,
left:410.3958435058594,
right:515.3958435058594,
top:689.0208435058594
},
{
bottom:794.0208435058594,
left:521.3958740234375,
right:626.3958740234375,
top:689.0208435058594
},
{
bottom:683.3750152587891,
left:78.39583587646484,
right:183.39583587646484,
top:634.3750152587891
},
{
bottom:684.6041870117188,
left:189.39584350585938,
right:349.3958435058594,
top:523.6041870117188
},
{
bottom:684.6041870117188,
left:355.3958435058594,
right:570.3958435058594,
top:523.6041870117188
},
{
bottom:629.0208435058594,
left:23.39583396911621,
right:183.3958339691162,
top:524.0208435058594
},
{
bottom:518.2395935058594,
left:23.39583396911621,
right:128.3958339691162,
top:302.2395935058594
},
{
bottom:517.8854217529297,
left:134.39584350585938,
right:405.3958435058594,
top:246.8854217529297
},
{
bottom:518.604175567627,
left:411.3958435058594,
right:626.3958435058594,
top:357.60417556762695
}
];
array.sort(function(a, b) {
if(a.bottom < b.top || a.left > b.right)
return 1;
if(a.bottom > b.top || a.left < b.right)
return -1;
return 0;
});
console.log(array[4],array[8]);
编辑:找到适合我的目的的解决方法。我使用 forEach
将项目相互比较并根据垂直顺序和水平顺序递增 z-index
:
function setTileZIndex() {
var $tiles = $('.grid__item__wrap');
var coords = [];
$tiles.each(function(index) {
var topLeft = $(this).offset();
var obj = {
bottom: topLeft.top + $(this).height(),
left: topLeft.left,
top: topLeft.top,
right: topLeft.left + $(this).width(),
$this: $(this),
z: 9999
};
coords.push(obj);
});
coords.forEach(function(a) {
coords.forEach(function(b) {
if (a.bottom < b.top)
b.z += 4;
if (a.left > b.right)
b.z += 1;
})
});
coords.forEach(function(elt) {
elt.$this.css('z-index', elt.z);
});
}
最佳答案
编辑
看来您在最初的问题中过于简单化了。这是更新后的答案:
I want a to come before b if a is below or to the left of b. Any help??
在这种情况下,请确保您比较的是每个对象的相同边缘——即比较 a.left
与 b.left
和 a。底部
与 b.bottom
...
const data = [
{ bottom:1181, left:23, right:72, top:910, },
{ bottom:906, left:23, right:183, top:801 },
{ bottom:1181, left:78, right:183, top:1132 },
{ bottom:1182, left:189, right:349, top:1021 },
{ bottom:1133, left:355, right:632, top:1132 },
{ bottom:795, left:78, right:183, top:690 },
{ bottom:1181, left:355, right:626, top:1132 },
{ bottom:1127, left:78, right:183, top:1022 },
{ bottom:1127, left:355, right:460, top:1022 },
{ bottom:1127, left:466, right:571, top:1022, },
{ bottom:1016, left:78, right:183, top:911 },
]
data.sort((a,b) => {
if (a.left < b.left || a.bottom < b.bottom)
return -1
else if (a.right > b.right || a.top > b.top)
return 1
else
return 0
})
console.log(data)
// [ { bottom: 906, left: 23, right: 183, top: 801 },
// { bottom: 1181, left: 23, right: 72, top: 910 },
// { bottom: 795, left: 78, right: 183, top: 690 },
// { bottom: 1016, left: 78, right: 183, top: 911 },
// { bottom: 1127, left: 78, right: 183, top: 1022 },
// { bottom: 1182, left: 189, right: 349, top: 1021 },
// { bottom: 1133, left: 355, right: 632, top: 1132 },
// { bottom: 1181, left: 78, right: 183, top: 1132 },
// { bottom: 1127, left: 355, right: 460, top: 1022 },
// { bottom: 1181, left: 355, right: 626, top: 1132 },
// { bottom: 1127, left: 466, right: 571, top: 1022 } ]
原始答案
我确定这个问题已经在这个网站的其他地方得到了回答,但是你的比较器必须返回-1
、0
和1
值以获得预期结果
-1
将a
移动到b
的左侧
1
将a
移动到b
的右侧
0
不会导致a
或b
改变位置
let sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort((a,b) => {
if (a < b)
return -1
else if (a > b)
return 1
else
return 0
})
console.log(sorted)
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
或者使用 super 简洁但更难阅读的链式三元表达式
let sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort((a,b) =>
a < b ? -1 : a > b ? 1 : 0
)
console.log(sorted)
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
请记住,数组中的元素不一定按照您可能期望的顺序进行比较——即,不要期望 compare(0,1)
然后是 compare(1,2 )
,然后是 compare(2,3)
,等等
let sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort((a,b) => {
console.log(a,b)
return a < b ? -1 : (a > b ? 1 : 0)
})
// 0 10
// 0 5
// 10 5
// 2 5
// 3 5
// 4 5
// 1 5
// 6 5
// 9 5
// 8 5
// 7 5
// 0 2
// 2 3
// 3 4
// 4 1
// 3 1
// 2 1
// 0 1
// 6 7
// 7 8
// 8 9
// 9 10
console.log(sorted)
//=> [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
关于javascript - Chrome Array.sorting 数字乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563981/