stable by time sort for same scores

This commit is contained in:
Umputun
2018-05-06 13:32:09 -05:00
parent a5c261caa7
commit b3aa7381ce
3 changed files with 20 additions and 7 deletions
+8 -2
View File
@@ -77,9 +77,15 @@ func (t *Tree) sortNodes(sortType string) {
case "+score", "-score", "score":
if strings.HasPrefix(sortType, "-") {
return t.Nodes[i].Comment.Score < t.Nodes[j].Comment.Score
if t.Nodes[i].Comment.Score == t.Nodes[j].Comment.Score {
return t.Nodes[i].Comment.Timestamp.After(t.Nodes[j].Comment.Timestamp)
}
return t.Nodes[i].Comment.Score > t.Nodes[j].Comment.Score
}
return t.Nodes[i].Comment.Score > t.Nodes[j].Comment.Score
if t.Nodes[i].Comment.Score == t.Nodes[j].Comment.Score {
return t.Nodes[i].Comment.Timestamp.Before(t.Nodes[j].Comment.Timestamp)
}
return t.Nodes[i].Comment.Score < t.Nodes[j].Comment.Score
default:
return t.Nodes[i].Comment.Timestamp.Before(t.Nodes[j].Comment.Timestamp)
+11 -4
View File
@@ -57,23 +57,30 @@ func TestMakeTreeSorts(t *testing.T) {
{ID: "22", ParentID: "2", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC)},
{ID: "4", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 0, time.UTC), Score: -2},
{ID: "3", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 100, time.UTC)},
{ID: "6", Timestamp: time.Date(2017, 12, 25, 19, 47, 22, 200, time.UTC)},
{ID: "5", Deleted: true},
}
res := MakeTree(comments, "time")
assert.Equal(t, "1", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "+time")
res = MakeTree(comments, "time")
assert.Equal(t, "1", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "-time")
assert.Equal(t, "3", res.Nodes[0].Comment.ID)
assert.Equal(t, "6", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "score")
assert.Equal(t, "2", res.Nodes[0].Comment.ID)
assert.Equal(t, "4", res.Nodes[0].Comment.ID)
assert.Equal(t, "3", res.Nodes[1].Comment.ID)
assert.Equal(t, "6", res.Nodes[2].Comment.ID)
assert.Equal(t, "1", res.Nodes[3].Comment.ID)
res = MakeTree(comments, "+score")
assert.Equal(t, "4", res.Nodes[0].Comment.ID)
res = MakeTree(comments, "-score")
assert.Equal(t, "4", res.Nodes[0].Comment.ID)
assert.Equal(t, "2", res.Nodes[0].Comment.ID)
}
func BenchmarkTree(b *testing.B) {
+1 -1
View File
@@ -1,6 +1,6 @@
### find request with tree
GET {{host}}/api/v1/find?site=remark&sort=-score&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/
GET {{host}}/api/v1/find?site=remark&sort=+score&format=tree&url=https://radio-t.com/p/2017/12/16/podcast-576/
### find request with plain
GET {{host}}/api/v1/find?site=remark&sort=-score&format=plain&url=https://radio-t.com/p/2017/12/16/podcast-576/