wangjia184 / sortedset

An ordered collection implemented in Golang with O(log(N)) time complexity on adding / searching / removing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

specify sorting order

jyzhou17 opened this issue · comments

is there any way I can specify sorting in ascending or descending order?

You can retrieve the node in ascending or descending order either

e.g.
Rank 1 means the first node;
Rank 2 means the second node;
Rank -1 means the last node;
Rank -2 means the second last node;

// get the node at rank 1 (the node with minimum score)
set.GetByRank(1, false)

// get & remove the node at rank -1 (the node with maximum score)
set.GetByRank(-1, true)

// get the node with the 2nd highest maximum score
set.GetByRank(-2, false)

// top ten highest score in descending order
set.GetByRankRange(-1, -10, false)

// top ten highest score in ascending order
set.GetByRankRange(-10, -1, false)

For score, you can retrieve the list in the order you want.

e.g.

// get the nodes whose score are within the interval [60,100], in ascending order 
set.GetByScoreRange(60, 100, nil)

// get the nodes whose score are within the interval [60,100], in descending order
set.GetByScoreRange(100, 60, nil)

You can retrieve the node in ascending or descending order either

e.g.
Rank 1 means the first node;
Rank 2 means the second node;
Rank -1 means the last node;
Rank -2 means the second last node;

// get the node at rank 1 (the node with minimum score)
set.GetByRank(1, false)

// get & remove the node at rank -1 (the node with maximum score)
set.GetByRank(-1, true)

// get the node with the 2nd highest maximum score
set.GetByRank(-2, false)

// top ten highest score in descending order
set.GetByRankRange(-1, -10, false)

// top ten highest score in ascending order
set.GetByRankRange(-10, -1, false)

For score, you can retrieve the list in the order you want.

e.g.

// get the nodes whose score are within the interval [60,100], in ascending order 
set.GetByScoreRange(60, 100, nil)

// get the nodes whose score are within the interval [60,100], in descending order
set.GetByScoreRange(100, 60, nil)

如果能增加一个参数表示升序还是降序就更好了