1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Stable sorting.
//
// Spliced into the `FluentIter` trait by `build.rs`. Names resolve in
// the crate-root scope of `lib.rs`, exactly as before the split.
/// ⇔ `.sort()` with the type's own ordering: collect and **stable**
/// sort. (JS would stringify non-comparable items; Rust uses `Ord`.)
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// assert_eq!([3, 1, 2].into_iter().sorted(), vec![1, 2, 3]);
/// ```
///
/// Resolving: nothing beats the minimum until the source is drained, so
/// a lazy sort wrapper can never short-circuit — the sorted `Vec` does
/// the same work once and adds re-iterability plus every `Vec` method
/// (npm's `IterableSorted` buffering made sense in JS, not here).
/// ⇔ `.sort(compareFn)`: stable sort with a custom comparator.
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// assert_eq!([1, 2, 3].into_iter().sort_with(|a, b| b.cmp(a)), vec![3, 2, 1]);
/// ```
///
/// Resolving, like [`FluentIter::sorted`].
/// ⇔ `.sortBy(keyFn)`: stable sort ascending by key. Ties keep encounter
/// order. As with `slice::sort_by_key` the key function may be called
/// more than once per element — keep it cheap, or sort decorated values.
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// let rows = [("b", 1), ("a", 2), ("b", 3)];
/// let sorted = rows.into_iter().sort_by(|(_, v)| *v);
/// assert_eq!(sorted, vec![("b", 1), ("a", 2), ("b", 3)]);
/// assert_eq!(sorted.first().copied(), Some(("b", 1)));
/// ```
///
/// Resolving, like [`FluentIter::sorted`] — but only pay for what you
/// need: if the goal is *the least* rather than the whole order, use
/// [`Iterator::min_by_key`] (or [`Iterator::max_by_key`]) directly.
/// One pass, no allocation, and std documents first-wins on ties —
/// the exact semantics of `sortBy(k).first()` over `.sort(k => k)` in
/// JS, without the `O(n log n)` sort and the intermediate `Vec`.
///
/// ```
/// use fluent_iterable::FluentIter; // for the sibling vocabulary
///
/// let rows = [("b", 1), ("a", 2), ("b", 3)];
/// // sortBy(v => v).first() ⇔ min_by_key, in O(n):
/// assert_eq!(rows.iter().min_by_key(|(_, v)| *v).copied(), Some(("b", 1)));
/// ```
/// ⇔ `.sortBy('-keyFn')` (the descending order descriptor): stable sort
/// descending by key; ties keep encounter order.
///
/// ```
/// use fluent_iterable::FluentIter;
///
/// let rows = [("b", 1), ("a", 2), ("b", 3)];
/// assert_eq!(
/// rows.into_iter().sort_by_desc(|(_, v)| *v),
/// vec![("b", 3), ("a", 2), ("b", 1)]
/// );
/// ```
///
/// Resolving, like [`FluentIter::sorted`].