diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-07-31 10:30:30 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-31 10:30:30 +0800 |
commit | d99bc645ee0138c6ea628f77c6475eec1a61253e (patch) | |
tree | ddf008608cc6fe8f64a46a531d36a6cf4392eeac /core/types/block.go | |
parent | 9261a3b9f3711ba736f2a143456e0b73bdfcfab8 (diff) | |
download | tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar.gz tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar.bz2 tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar.lz tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar.xz tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.tar.zst tangerine-consensus-d99bc645ee0138c6ea628f77c6475eec1a61253e.zip |
Add new sorting method for blocks
- Add types.ByHeight to sort slice of blocks by their heights.
- Add test case for sorting methods of types.Block.
Diffstat (limited to 'core/types/block.go')
-rw-r--r-- | core/types/block.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/core/types/block.go b/core/types/block.go index 217ea73..61b6535 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -87,3 +87,18 @@ func (b ByHash) Less(i int, j int) bool { func (b ByHash) Swap(i int, j int) { b[i], b[j] = b[j], b[i] } + +// ByHeight is the helper type for sorting slice of blocks by height. +type ByHeight []*Block + +func (b ByHeight) Len() int { + return len(b) +} + +func (b ByHeight) Less(i int, j int) bool { + return b[i].Height < b[j].Height +} + +func (b ByHeight) Swap(i int, j int) { + b[i], b[j] = b[j], b[i] +} |