@@ -129,37 +129,45 @@ function nextreme(ord::Base.Ordering, n::Int, arr::AbstractVector{T}) where T
129
129
end
130
130
131
131
"""
132
- nlargest(n, arr)
132
+ nlargest(n, arr; kw... )
133
133
134
134
Return the `n` largest elements of the array `arr`.
135
135
136
136
Equivalent to:
137
- sort(arr, order = Base.Reverse )[1:min(n, end)]
137
+ sort(arr, kw..., rev=true )[1:min(n, end)]
138
138
139
139
Note that if `arr` contains floats and is free of NaN values,
140
- then the following alternative may be used to achieve 2x performance.
140
+ then the following alternative may be used to achieve 2x performance:
141
+
141
142
DataStructures.nextreme(DataStructures.FasterReverse(), n, arr)
143
+
142
144
This faster version is equivalent to:
145
+
143
146
sort(arr, lt = >)[1:min(n, end)]
144
147
"""
145
- function nlargest (n:: Int , arr:: AbstractVector )
146
- return nextreme (Base. Reverse, n, arr)
148
+ function nlargest (n:: Int , arr:: AbstractVector ; lt= isless, by= identity)
149
+ order = Base. ReverseOrdering (Base. ord (lt, by, nothing ))
150
+ return nextreme (order, n, arr)
147
151
end
148
152
149
153
"""
150
- nsmallest(n, arr)
154
+ nsmallest(n, arr; kw... )
151
155
152
156
Return the `n` smallest elements of the array `arr`.
153
157
154
158
Equivalent to:
155
- sort(arr, order = Base.Forward )[1:min(n, end)]
159
+ sort(arr; kw... )[1:min(n, end)]
156
160
157
161
Note that if `arr` contains floats and is free of NaN values,
158
- then the following alternative may be used to achieve 2x performance.
162
+ then the following alternative may be used to achieve 2x performance:
163
+
159
164
DataStructures.nextreme(DataStructures.FasterForward(), n, arr)
165
+
160
166
This faster version is equivalent to:
167
+
161
168
sort(arr, lt = <)[1:min(n, end)]
162
169
"""
163
- function nsmallest (n:: Int , arr:: AbstractVector )
164
- return nextreme (Base. Forward, n, arr)
170
+ function nsmallest (n:: Int , arr:: AbstractVector ; lt= isless, by= identity)
171
+ order = Base. ord (lt, by, nothing )
172
+ return nextreme (order, n, arr)
165
173
end
0 commit comments