-
Notifications
You must be signed in to change notification settings - Fork 48
Open
Labels
Description
Add support of SortFunc similar to MatchFunc.
It would be convenient when need to use some third-party libraries types.
Example:
package main
import (
"fmt"
"log"
"github.com/shopspring/decimal"
"github.com/timshannon/bolthold"
bolt "go.etcd.io/bbolt"
)
type Item struct {
Name string `boltholdKey:"Name"`
Score decimal.Decimal
}
func ErrorCheck(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
data := []Item{
{
Name: "Blue",
Score: decimal.NewFromInt(7),
},
{
Name: "Red",
Score: decimal.NewFromInt(5),
},
{
Name: "Green",
Score: decimal.NewFromInt(15),
},
{
Name: "Yellow",
Score: decimal.NewFromInt(9),
},
{
Name: "White",
Score: decimal.NewFromInt(6),
},
}
filename := "tempfile"
store, err := bolthold.Open(filename, 0666, nil)
defer store.Close()
ErrorCheck(err)
ErrorCheck(store.Bolt().Update(func(tx *bolt.Tx) error {
for i := range data {
err := store.TxUpsert(tx, data[i].Name, data[i])
if err != nil {
return err
}
}
return nil
}))
var result []Item
q := &bolthold.Query{}
ErrorCheck(store.Find(&result, q.SortBy("Score")))
for i := range result {
fmt.Println(result[i].Name, result[i].Score)
}
}
Output (items compared lexicographically):
Green 15
Red 5
White 6
Blue 7
Yellow 9
Reactions are currently unavailable