Skip to content

Instantly share code, notes, and snippets.

@axiaoxin
Created October 28, 2021 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiaoxin/c2dcc79d9c28b21628b427eb14ff1a44 to your computer and use it in GitHub Desktop.
Save axiaoxin/c2dcc79d9c28b21628b427eb14ff1a44 to your computer and use it in GitHub Desktop.
sort by time and top
package main
import (
"fmt"
"sort"
)
type Item struct {
Top int
Value string
StartTs int
}
type List []Item
func st(l List) {
sort.Slice(l, func(i, j int) bool {
return l[i].StartTs < l[j].StartTs
})
sort.Slice(l, func(i, j int) bool {
if l[i].Top == 1 && l[j].Top == 1 {
return l[i].StartTs < l[j].StartTs
}
return l[i].Top == 1
})
}
func main() {
i1 := Item{
Top: 1,
Value: "i1",
StartTs: 10,
}
i2 := Item{
Top: 1,
Value: "i2",
StartTs: 12,
}
i3 := Item{
Top: 0,
Value: "i3",
StartTs: 9,
}
i4 := Item{
Top: 1,
Value: "i4",
StartTs: 11,
}
l := List{i1, i2, i3, i4}
fmt.Println(l)
st(l)
fmt.Println(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment