Skip to content

Commit

Permalink
pkg/list/sort: always use stable sort
Browse files Browse the repository at this point in the history
Change-Id: I11fd18901a1891c1fffaa93bb462a24d336792c1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7363
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Oct 7, 2020
1 parent 737a103 commit 99d18dc
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions pkg/list/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func (s *valueSorter) Less(i, j int) bool {
return isLess
}

// Sort sorts data. It does O(n*log(n)) comparisons.
// The sort is not guaranteed to be stable.
// Sort sorts data while keeping the original order of equal elements.
// It does O(n*log(n)) comparisons.
//
// cmp is a struct of the form {T: _, x: T, y: T, less: bool}, where
// less should reflect x < y.
Expand All @@ -67,15 +67,11 @@ func (s *valueSorter) Less(i, j int) bool {
func Sort(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error) {
s := valueSorter{list, cmp, nil}
// The input slice is already a copy and that we can modify it safely.
sort.Sort(&s)
sort.Stable(&s)
return s.ret()
}

// SortStable sorts data while keeping the original order of equal elements.
// It does O(n*log(n)) comparisons.
//
// See Sort for an example usage.
//
// Deprecated: use Sort, which is always stable
func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error) {
s := valueSorter{list, cmp, nil}
sort.Stable(&s)
Expand Down

0 comments on commit 99d18dc

Please sign in to comment.