我有以下代码:
let arr = [];
for (i = 14; i <= 31; i++) {
let d = "2018-08-" + String(i);
arr.push({
date: d
});
}
arr.sort((a, b) => a.date - b.date);
console.log(arr);
sort()
应该仅与数字一起使用-
是一个坏主意这个错误代码让我着迷:结果。
从另一个字符串中减去一个字符串得到NaN
,所以我希望数组保持不变(14, 15, 16, 17... 31
),或者完全翻转(31, 30, 29, 28... 14
)。 / p>
相反,实际(一致)结果是
我很好奇为什么知道sort()
正输出该字符串序列。为什么31
,15
和23
被移动,为什么它们被移动到那些特定位置?
答案 0 :(得分:3)
使用更简单的数组可能更容易理解此行为。
例如:
let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
arr.sort(() => NaN)
console.log(arr)
在chrome中,这将返回一个数组顺序,例如:[0,11,2,3,4,5,1,7,8,9,10,6]
。
这很特殊,但是如果您查看V8代码中的排序实现,则会发现quicksort和insert sort混合在一起。实际上,如果递归调用quicksort直到要递归的数组的长度小于10,则它将切换为插入排序。
快速排序选择枢轴的方式说明了您所看到的行为。这是一个片段,其中包含来自V8的略微被截断的代码:
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
function comparefn(a,b){
return NaN
}
function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; i++) {
var element = a[i];
for (var j = i - 1; j >= from; j--) {
var tmp = a[j];
var order = comparefn(tmp, element);
if (order > 0) {
a[j + 1] = tmp;
} else {
break;
}
}
a[j + 1] = element;
}
};
function QuickSort(a, from, to) {
var third_index = 0;
while (true) {
// Insertion sort is faster for short arrays.
if (to - from <= 10) {
InsertionSort(a, from, to);
return;
}
third_index = from + ((to - from) >> 1);
// Find a pivot as the median of first, last and middle element.
var v0 = a[from];
var v1 = a[to - 1];
var v2 = a[third_index];
var c01 = comparefn(v0, v1);
if (c01 > 0) {
// v1 < v0, so swap them.
var tmp = v0;
v0 = v1;
v1 = tmp;
} // v0 <= v1.
var c02 = comparefn(v0, v2);
if (c02 >= 0) {
// v2 <= v0 <= v1.
var tmp = v0;
v0 = v2;
v2 = v1;
v1 = tmp;
} else {
// v0 <= v1 && v0 < v2
var c12 = comparefn(v1, v2);
if (c12 > 0) {
// v0 <= v2 < v1
var tmp = v1;
v1 = v2;
v2 = tmp;
}
}
// v0 <= v1 <= v2
a[from] = v0;
a[to - 1] = v2;
var pivot = v1;
var low_end = from + 1; // Upper bound of elements lower than pivot.
var high_start = to - 1; // Lower bound of elements greater than pivot.
a[third_index] = a[low_end];
a[low_end] = pivot;
// From low_end to i are elements equal to pivot.
// From i to high_start are elements that haven't been compared yet.
partition: for (var i = low_end + 1; i < high_start; i++) {
var element = a[i];
var order = comparefn(element, pivot);
if (order < 0) {
a[i] = a[low_end];
a[low_end] = element;
low_end++;
} else if (order > 0) {
do {
high_start--;
if (high_start == i) break partition;
var top_elem = a[high_start];
order = comparefn(top_elem, pivot);
} while (order > 0);
a[i] = a[high_start];
a[high_start] = element;
if (order < 0) {
element = a[i];
a[i] = a[low_end];
a[low_end] = element;
low_end++;
}
}
}
if (to - high_start < low_end - from) {
QuickSort(a, high_start, to);
to = low_end;
} else {
QuickSort(a, from, low_end);
from = high_start;
}
}
};
// run it
QuickSort(arr, 0, arr.length)
console.log(arr)
如果您查看此内容,尤其是选择枢轴的方式以及当它切换为插入排序时,您将看到为什么按原样对结果进行排序。
当compare函数始终返回NaN时,将跳过代码中的所有if
,如下所示:
var c12 = comparefn(v1, v2);
if (c12 > 0) { /* etc /*}
意味着整体减少到较小:
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
function comparefn(a,b){
//console.log(a, b)
return NaN
}
function QuickSort(a, from, to) {
var third_index = 0;
while (true) {
// Insertion sort is faster for short arrays.
if (to - from <= 10) {
return;
}
third_index = from + ((to - from) >> 1);
// Find a pivot as the median of first, last and middle element.
var v0 = a[from];
var v1 = a[to - 1];
var v2 = a[third_index];
a[from] = v0;
a[to - 1] = v2;
var pivot = v1;
var low_end = from + 1; // Upper bound of elements lower than pivot.
var high_start = to - 1; // Lower bound of elements greater than pivot.
a[third_index] = a[low_end];
a[low_end] = pivot;
partition: for (var i = low_end + 1; i < high_start; i++) {
var element = a[i];
}
if (to - high_start < low_end - from) {
QuickSort(a, high_start, to);
to = low_end;
} else {
QuickSort(a, from, low_end);
from = high_start;
}
}
};
QuickSort(arr, 0, arr.length)
console.log(arr)