2014-06-07 03:10:15 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2014-07-12 21:06:48 +00:00
|
|
|
"github.com/calmh/syncthing/protocol"
|
2014-07-15 11:04:37 +00:00
|
|
|
"sort"
|
2014-06-07 03:10:15 +00:00
|
|
|
)
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
type SortBy func(p protocol.FileInfo) int
|
2014-06-07 03:10:15 +00:00
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
func (by SortBy) Sort(files []protocol.FileInfo) {
|
2014-06-07 03:10:15 +00:00
|
|
|
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
|
2014-06-07 03:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|