@@ -59,20 +59,20 @@ func (srv *TodoServer) ListTodos(ctx context.Context, req *connect.Request[todov
|
||||
Done: BoolPtr(todo.Done.Bool),
|
||||
})
|
||||
}
|
||||
slices.SortFunc(responseTodos, func(a, b *todov1.Todo) int {
|
||||
dateCmp := cmp.Compare(a.GetCreatedAt(), b.GetCreatedAt())
|
||||
return dateCmp
|
||||
})
|
||||
slices.SortFunc(responseTodos, func(a, b *todov1.Todo) int {
|
||||
var boolToInt = func(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
// Open todos first, each group oldest first. A single total order (SortFunc isn't stable)
|
||||
// keeps rows from swapping places between polls.
|
||||
boolToInt := func(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
doneCmp := cmp.Compare(boolToInt(*a.Done), boolToInt(*b.Done))
|
||||
return doneCmp
|
||||
return 0
|
||||
}
|
||||
slices.SortFunc(responseTodos, func(a, b *todov1.Todo) int {
|
||||
return cmp.Or(
|
||||
cmp.Compare(boolToInt(a.GetDone()), boolToInt(b.GetDone())),
|
||||
cmp.Compare(a.GetCreatedAt(), b.GetCreatedAt()),
|
||||
cmp.Compare(a.GetId(), b.GetId()),
|
||||
)
|
||||
})
|
||||
|
||||
return &connect.Response[todov1.ListTodosResponse]{
|
||||
|
||||
Reference in New Issue
Block a user