restore time sort, add active sort

This commit is contained in:
Umputun
2018-05-16 11:43:14 -05:00
parent 4cf7c0a5ee
commit 10c92a9251
3 changed files with 14 additions and 3 deletions
+6
View File
@@ -88,6 +88,12 @@ func (t *Tree) sortNodes(sortType string) {
sort.Slice(t.Nodes, func(i, j int) bool {
switch sortType {
case "+time", "-time", "time":
if strings.HasPrefix(sortType, "-") {
return t.Nodes[i].Comment.Timestamp.After(t.Nodes[j].Comment.Timestamp)
}
return t.Nodes[i].Comment.Timestamp.Before(t.Nodes[j].Comment.Timestamp)
case "+active", "-active", "active":
if strings.HasPrefix(sortType, "-") {
return t.Nodes[i].ts.After(t.Nodes[j].ts)
}
+7 -2
View File
@@ -61,14 +61,19 @@ func TestTreeSortNodes(t *testing.T) {
{ID: "5", Deleted: true},
}
res := MakeTree(comments, "+time")
res := MakeTree(comments, "+active")
assert.Equal(t, "2", res.Nodes[0].Comment.ID)
t.Log(res.Nodes[0].Comment.ID, res.Nodes[0].ts)
res = MakeTree(comments, "-time")
res = MakeTree(comments, "-active")
t.Log(res.Nodes[0].Comment.ID, res.Nodes[0].ts)
assert.Equal(t, "1", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "+time")
t.Log(res.Nodes[0].Comment.ID, res.Nodes[0].ts)
res = MakeTree(comments, "-time")
assert.Equal(t, "6", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "score")
assert.Equal(t, "4", res.Nodes[0].Comment.ID)
assert.Equal(t, "3", res.Nodes[1].Comment.ID)
+1 -1
View File
@@ -37,7 +37,7 @@ type Admin interface {
func sortComments(comments []Comment, sortFld string) []Comment {
sort.Slice(comments, func(i, j int) bool {
switch sortFld {
case "+time", "-time", "time":
case "+time", "-time", "time", "+active", "-active", "active":
if strings.HasPrefix(sortFld, "-") {
return comments[i].Timestamp.After(comments[j].Timestamp)
}