syncthing/files/sort.go

34 lines
567 B
Go
Raw Normal View History

package files
import (
2014-07-12 21:06:48 +00:00
"github.com/calmh/syncthing/protocol"
"sort"
)
2014-07-12 21:06:48 +00:00
type SortBy func(p protocol.FileInfo) int
2014-07-12 21:06:48 +00:00
func (by SortBy) Sort(files []protocol.FileInfo) {
ps := &fileSorter{
files: files,
by: by,
}
sort.Sort(ps)
}
type fileSorter struct {
2014-07-12 21:06:48 +00:00
files []protocol.FileInfo
by func(p1 protocol.FileInfo) int
}
func (s *fileSorter) Len() int {
return len(s.files)
}
func (s *fileSorter) Swap(i, j int) {
s.files[i], s.files[j] = s.files[j], s.files[i]
}
func (s *fileSorter) Less(i, j int) bool {
return s.by(s.files[i]) > s.by(s.files[j])
}