Skip to content

Commit

Permalink
Merge pull request #2006 from thePHTest/sort_with_indices
Browse files Browse the repository at this point in the history
fix sort_by_with_indices for zero and one length slices
  • Loading branch information
Kelimion authored Sep 1, 2022
2 parents c2423dc + 3f3ae4b commit b7ac0a9
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/slice/sort.odin
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
// sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j"
// This sort is not guaranteed to be stable
sort_by_with_indices :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool, allocator := context.allocator) -> (indices : []int) {
indices = make([]int, len(data), allocator)
when size_of(E) != 0 {
if n := len(data); n > 1 {
indices = make([]int, len(data), allocator)
for _, idx in indices {
indices[idx] = idx
}
_quick_sort_general_with_indices(data, indices, 0, n, _max_depth(n), less, .Less)
return indices
}
}
return nil
return indices
}

sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
Expand Down

0 comments on commit b7ac0a9

Please sign in to comment.