数据排序
¥Sorting data
对值进行排序;另见 bisect。
¥Sort values; see also bisect.
ascending(a, b)
示例 · 源代码 · 如果 a 小于 b,则返回 -1;如果 a 大于 b,则返回 1;如果 a 和 b 相等,则返回 0;否则返回 NaN。
¥Examples · Source · Returns -1 if a is less than b, 1 if a is greater than b, 0 if a and b are equivalent, and otherwise NaN.
[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;如果 a 和 b 相等,则返回 0;否则返回 NaN。
¥Examples · Source · Returns -1 if a is greater than b, 1 if a is less than b, 0 if a and b are equivalent, and otherwise NaN.
[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)
示例 · 源代码 · 使用指定的可迭代键返回指定源数组或对象的排列。返回的数组按顺序包含源对象中每个键的对应属性。
¥Examples · Source · Returns a permutation of the specified source array or object using the specified iterable of keys. The returned array contains the corresponding property of the source object for each key in keys, in order.
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
const object = {yield: 27, variety: "Manchuria", year: 1931, site: "University Farm"};
可以像这样提取三个字段
¥three fields could be extract like so
d3.permute(object, ["site", "variety", "yield"]) // ["University Farm", "Manchuria", 27]
quickselect(array, k, lo, hi, compare)
示例 · 源代码 · 重新排列数组中 lo 和 hi(含)之间的元素,使得 array[k] 等于 (k - lo + 1) 最小值,array.slice(lo, k) 根据给定的比较函数获取 k 个最小元素,并返回给定的数组。如果未指定 lo,则默认为零;如果未指定 hi,则默认为 array.length - 1;如果未指定 compare,则默认为 ascending。
¥Examples · Source · Rearranges the elements of array between lo and hi (inclusive) in-place such that array[k] is the (k - lo + 1)-th smallest value and array.slice(lo, k) are the k smallest elements, according to the given compare function, and returns the given array. If lo is not specified, it defaults to zero; if hi is not specified, it defaults to array.length - 1; if compare is not specified, it defaults to ascending.
例如,给定一个数字数组:
¥For example, given an array of numbers:
const numbers = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];
要选择最小的 8 个元素:
¥To select the smallest 8 elements:
d3.quickselect(numbers, 8)
重新排列的数字为:
¥The rearranged numbers is
[39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
// ^^ numbers[k]
其中 numbers[8] 为 53:大于前面的 k 个元素且小于后面的元素。由 Volodymyr Agafonkin 的快速选择 实现。
¥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)
源代码 · 返回一个数组,其中包含给定可迭代对象中的值,这些值以相反的顺序排列。
¥Source · Returns an array containing the values in the given iterable in reverse order.
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 随机排序 就地随机化指定数组的顺序并返回该数组。
¥Examples · Source · Randomizes the order of the specified array in-place using the Fisher–Yates shuffle and returns the array.
d3.shuffle([..."abcdefg"]) // ["e", "c", "a", "d", "b", "g", "f"], perhaps
如果指定了 start,则它是要随机排列的数组的起始索引(含);如果未指定 start,则默认为零。如果指定了 stop,则它是要随机排列的数组的结束索引(不包括);如果未指定 stop,则默认为 array.length。例如,要对数组的前十个元素进行打乱: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)
源代码 · 返回给定指定随机源的 shuffle 函数。
¥Source · Returns a shuffle function given the specified random source.
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 一起使用,以实现确定性的 shuffle。
¥Often used with d3.randomLcg for a deterministic shuffle.
sort(iterable, comparator)
源代码 · 返回一个数组,其中包含给定可迭代对象中的值,这些值按照给定比较器或访问器函数定义的排序顺序排列。如果未指定 comparator,则默认为 d3.ascending。
¥Source · Returns an array containing the values in the given iterable in the sorted order defined by the given comparator or accessor function. If comparator is not specified, it defaults to d3.ascending.
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,
d3.sort(data, (d) => d.value)
它相当于使用 自然顺序 的比较器:
¥it is equivalent to a comparator using natural order:
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.
d3.sort(points, ({x}) => x, ({y}) => y)
以上内容等同于:
¥The above is equivalent to:
d3.sort(data, (a, b) => d3.ascending(a.x, b.x) || d3.ascending(a.y, b.y))
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.