mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-07 19:56:18 -04:00
Tighten race result ingestion ordering
This commit is contained in:
20
internal/models/sort.go
Normal file
20
internal/models/sort.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
import "sort"
|
||||
|
||||
// SortSessionResults orders results by classified position, with position 0 (DNF/DNS) last.
|
||||
func SortSessionResults(results []SessionResult) {
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
pi, pj := results[i].Position, results[j].Position
|
||||
if pi == 0 {
|
||||
pi = 9999
|
||||
}
|
||||
if pj == 0 {
|
||||
pj = 9999
|
||||
}
|
||||
if pi != pj {
|
||||
return pi < pj
|
||||
}
|
||||
return results[i].DriverNumber < results[j].DriverNumber
|
||||
})
|
||||
}
|
||||
21
internal/models/sort_test.go
Normal file
21
internal/models/sort_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSortSessionResults(t *testing.T) {
|
||||
results := []SessionResult{
|
||||
{DriverNumber: 55, Position: 0, DNF: true},
|
||||
{DriverNumber: 10, Position: 0, DNS: true},
|
||||
{DriverNumber: 1, Position: 1},
|
||||
{DriverNumber: 44, Position: 2},
|
||||
}
|
||||
|
||||
SortSessionResults(results)
|
||||
|
||||
want := []int{1, 44, 10, 55}
|
||||
for i, dn := range want {
|
||||
if results[i].DriverNumber != dn {
|
||||
t.Fatalf("results[%d].DriverNumber = %d, want %d", i, results[i].DriverNumber, dn)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user