Skip to content

数据排序

🌐 Sorting data

排序值;另见 bisect

🌐 Sort values; see also bisect.

ascending(a, b)

示例 · 来源 · 如果 a 小于 b 返回 -1,如果 a 大于 b 返回 1,如果 ab 相等返回 0,否则返回 NaN。

js
[39, 21, 1, 104, 22].sort(d3.ascending) // [1, 21, 22, 39, 104]

这是用于自然顺序的比较函数,可以与 array.sort 一起使用,将元素按升序排列。

🌐 This is the comparator function for natural order, and can be used with array.sort to arrange elements in ascending order.

descending(a, b)

示例 · 来源 · 如果 a 大于 b 返回 -1,如果 a 小于 b 返回 1,如果 ab 相等返回 0,否则返回 NaN。

js
[39, 21, 1, 104, 22].sort(d3.descending) // [104, 39, 22, 21, 1]

这是用于自然顺序的比较函数,可以与 array.sort 一起使用以按降序排列元素。

🌐 This is the comparator function for natural order, and can be used with array.sort to arrange elements in descending order.

permute(source, keys)

示例 · 来源 · 使用指定的 keys 可迭代对象返回指定 source 数组或对象的一个排列。返回的数组按 keys 中每个键的顺序包含源对象的对应属性。

js
d3.permute(["a", "b", "c"], [1, 2, 0]) // returns ["b", "c", "a"]

给定的不必是数组;例如,给定一个对象

🌐 The given source need not be an array; for example, given an object

js
const object = {yield: 27, variety: "Manchuria", year: 1931, site: "University Farm"};

可以像这样提取三个字段

🌐 three fields could be extract like so

js
d3.permute(object, ["site", "variety", "yield"]) // ["University Farm", "Manchuria", 27]

quickselect(array, k, lo, hi, compare)

示例 · 来源 · 在原地重新排列 array 中从 lohi(包括两端)的元素,使得 array[k] 是第 (k - lo + 1) 小的值,并且 array.slice(lo, k) 是最小的 k 个元素,根据给定的 compare 函数,并返回给定的 array。如果未指定 lo,则默认为零;如果未指定 hi,则默认为 array.length - 1;如果未指定 compare,则默认为 升序

例如,给定一个数字数组:

🌐 For example, given an array of numbers:

js
const numbers = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];

要选择最小的 8 个元素:

🌐 To select the smallest 8 elements:

js
d3.quickselect(numbers, 8)

重新排列的数字

🌐 The rearranged numbers is

js
[39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
//                               ^^ numbers[k]

其中 numbers[8] 是 53:大于前面的 k 个元素且小于后面的元素。由 Volodymyr Agafonkin 的 quickselect 实现。

🌐 where numbers[8] is 53: greater than the preceding k elements and less than the following elements. Implemented by Volodymyr Agafonkin’s quickselect.

reverse(iterable)

来源 · 返回一个数组,其中包含给定可迭代对象中的值,顺序为反向。

js
d3.reverse(new Set([0, 2, 3, 1])) // [1, 3, 2, 0]

相当于 array.reverse,只是它不会改变给定的输入,并且适用于任何可迭代对象。

🌐 Equivalent to array.reverse, except that it does not mutate the given input and works with any iterable.

shuffle(array, start, stop)

示例 · 来源 · 使用 Fisher–Yates 洗牌 就地随机排列指定的 数组 的顺序,并返回该 数组

js
d3.shuffle([..."abcdefg"]) // ["e", "c", "a", "d", "b", "g", "f"], perhaps

如果指定了 start,它是要打乱的 array 的起始索引(包含);如果未指定 start,默认为零。如果指定了 stop,它是要打乱的 array 的结束索引(不包含);如果未指定 stop,默认为 array.length。例如,要打乱 array 的前十个元素:shuffle(array, 0, 10)。

🌐 If start is specified, it is the starting index (inclusive) of the array to shuffle; if start is not specified, it defaults to zero. If stop is specified, it is the ending index (exclusive) of the array to shuffle; if stop is not specified, it defaults to array.length. For example, to shuffle the first ten elements of the array: shuffle(array, 0, 10).

shuffler(random)

来源 · 根据指定的随机源返回一个洗牌函数

js
d3.shuffler(d3.randomLcg(42))([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) // [5, 3, 7, 6, 8, 9, 1, 4, 0, 2]

经常与 d3.randomLcg 一起使用以实现确定性洗牌。

🌐 Often used with d3.randomLcg for a deterministic shuffle.

sort(iterable, comparator)

来源 · 返回一个数组,包含给定 可迭代对象 中的值,按照给定的 比较器访问器 函数定义的排序顺序排列。如果未指定 比较器,则默认为 d3.ascending

js
d3.sort(new Set([0, 2, 3, 1])) // [0, 1, 2, 3]

如果指定了一个访问器(一个不正好接收两个参数的函数),

🌐 If an accessor (a function that does not take exactly two arguments) is specified,

js
d3.sort(data, (d) => d.value)

它相当于使用自然顺序比较器

🌐 it is equivalent to a comparator using natural order:

js
d3.sort(data, (a, b) => d3.ascending(a.value, b.value))

访问器 每个元素只调用一次,因此即使访问器是非确定性的,返回的排序顺序也是一致的。可以指定多个访问器以打破平局。

🌐 The accessor is only invoked once per element, and thus the returned sorted order is consistent even if the accessor is nondeterministic. Multiple accessors may be specified to break ties.

js
d3.sort(points, ({x}) => x, ({y}) => y)

以上内容等同于:

🌐 The above is equivalent to:

js
d3.sort(data, (a, b) => d3.ascending(a.x, b.x) || d3.ascending(a.y, b.y))

array.sort 不同,d3.sort 不会修改给定的输入,比较函数的默认顺序是自然顺序而不是字典顺序,并且输入可以是任何可迭代对象。

🌐 Unlike array.sort, d3.sort does not mutate the given input, the comparator defaults to natural order instead of lexicographic order, and the input can be any iterable.