Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Uses go 1.8 function sort.Slice
  • Loading branch information
matttamatic committed Jan 25, 2019
1 parent 50f74a1 commit f2f76d4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"strconv"
"sort"

"github.com/gorilla/pat"
"github.com/ian-kent/go-log/log"
Expand Down Expand Up @@ -115,6 +116,11 @@ func (apiv2 *APIv2) messages(w http.ResponseWriter, req *http.Request) {
res.Items = []data.Message(*messages)
res.Total = apiv2.config.Storage.Count()

log.Println("Attempting sort")
sort.Slice(res.Items, func(i, j int) bool{
return res.Items[i].Created.After(res.Items[j].Created)
})

bytes, _ := json.Marshal(res)
w.Header().Add("Content-Type", "text/json")
w.Write(bytes)
Expand Down

1 comment on commit f2f76d4

@matttamatic
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is basically the same as https://github.com/mailhog/storage/pull/9/files but doing the sort in the API instead of the storage. Might be better to let the storage implement sort instead so those layers can offload the sort operation to mongo or whatever.

Please sign in to comment.