Skip to content

Commit

Permalink
[#144] Use stable sort (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta authored Aug 29, 2024
1 parent 32e7c65 commit 601d735
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package donburi

import (
"cmp"
"iter"
"sort"
"slices"
"sync"

"github.com/yohamta/donburi/filter"
Expand Down Expand Up @@ -87,9 +88,12 @@ func (q *OrderedQuery[T]) IterOrdered(w World, orderBy *ComponentType[T]) iter.S
archetype.Unlock()
}

// Sort all entries
sort.Slice(q.entries, func(i, j int) bool {
return orderBy.GetValue(q.entries[i]).Order() < orderBy.GetValue(q.entries[j]).Order()
// Sort all entries using a stable sort
slices.SortStableFunc(q.entries, func(a, b *Entry) int {
return cmp.Compare(
orderBy.GetValue(a).Order(),
orderBy.GetValue(b).Order(),
)
})

// Yield sorted entries
Expand Down

0 comments on commit 601d735

Please sign in to comment.