diff --git a/hbrtl/database.go b/hbrtl/database.go index b8b9949..4bdce44 100644 --- a/hbrtl/database.go +++ b/hbrtl/database.go @@ -10,6 +10,7 @@ package hbrtl import ( "fmt" "os" + "sort" "strings" "five/hbrt" @@ -1914,14 +1915,12 @@ func rtlDbUpdate(t *hbrt.Thread) { t.RetBool(true) } -// stableSort is a tiny insertion sort for small N (typical DBF SORT -// targets are interactive datasets). Avoids a sort import dependency. +// stableSort sorts rows in place via the stdlib's `sort.SliceStable` +// — O(n log n) with stable ordering preserved for equal keys. The +// previous insertion-sort implementation degraded to O(n²) and was +// unusable for DBFs over a few thousand rows. func stableSort(rows [][]hbrt.Value, less func(i, j int) bool) { - for i := 1; i < len(rows); i++ { - for j := i; j > 0 && less(j, j-1); j-- { - rows[j], rows[j-1] = rows[j-1], rows[j] - } - } + sort.SliceStable(rows, less) } // --- DBSETFILTER / DBCLEARFILTER / DBFILTER ---