Golang's sort package provides a set of primitives that allows us to define the order of our own structures. Furthermore we can define various ways of sorting the structures by extending the base collection.
type Order struct {
Ind string // buy sell indicator
Epic string
Price float64
Quantity float64
Time int64
}
type Orders []Order
func (o Orders) Len() int {
return len(o)
}
func (o Orders) Swap(i, j int) {
o[i], o[j] = o[j], o[i]
}
type BuyOrders struct {
Orders
}
type SellOrders struct {
Orders
}
func (o BuyOrders) Less(i, j int) bool {
// buy orders are reversed
return true
}
return false
}
// because of this element we cannot simply call reverse
return o.Orders[i].Time < o.Orders[j].Time
}
func (o SellOrders) Less(i, j int) bool {
return true
}
return false
}
return o.Orders[i].Time < o.Orders[j].Time
}