From c7a7688fdc7d6cee16d8b87cc660d376ed5f4c43 Mon Sep 17 00:00:00 2001 From: Yota Hamada Date: Thu, 29 Aug 2024 19:48:08 +0900 Subject: [PATCH] Use stable sort --- query.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/query.go b/query.go index 68568d6..a61f05a 100644 --- a/query.go +++ b/query.go @@ -1,8 +1,9 @@ package donburi import ( + "cmp" "iter" - "sort" + "slices" "sync" "github.com/yohamta/donburi/filter" @@ -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