Skip to content

Commit d97de50

Browse files
Filmbostock
authored andcommitted
document multiple accessors for d3.sort
1 parent f819290 commit d97de50

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ d3.reverse(new Set([0, 2, 3, 1])) // [1, 3, 2, 0]
690690
```
691691

692692
<a name="sort" href="#sort">#</a> d3.<b>sort</b>(<i>iterable</i>, <i>comparator</i> = d3.ascending) · [Source](https://github.com/d3/d3-array/blob/master/src/sort.js)
693-
<br><a name="sort" href="#sort">#</a> d3.<b>sort</b>(<i>iterable</i>, <i>accessor</i>)
693+
<br><a name="sort" href="#sort">#</a> d3.<b>sort</b>(<i>iterable</i>, ...<i>accessors</i>)
694694

695695
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](#ascending). Equivalent to [*array*.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort), except that it does not mutate the given *iterable*, and the comparator defaults to natural order instead of lexicographic order:
696696

@@ -710,7 +710,19 @@ it is equivalent to a *comparator* using [natural order](#ascending):
710710
d3.sort(data, (a, b) => d3.ascending(a.value, b.value))
711711
```
712712

713-
The *accessor* is only invoked once per element, and thus may be nondeterministic.
713+
The *accessor* is only invoked once per element, and thus the returned sorted order is consistent even if the accessor is nondeterministic.
714+
715+
Multiple accessors may be specified to break ties:
716+
717+
```js
718+
d3.sort(points, ({x}) => x, ({y}) => y)
719+
```
720+
721+
This is equivalent to:
722+
723+
```js
724+
d3.sort(data, (a, b) => d3.ascending(a.x, b.x) || d3.ascending(a.y, b.y))
725+
```
714726

715727
### Sets
716728

0 commit comments

Comments
 (0)