restore time sort, add active sort
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user