summaryrefslogtreecommitdiff
path: root/repo.SortByAge.go
blob: 9e73eefa96bbdfcb3c20235d70f427cb445fab62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gitpb

import "sort"

func (x *Repos) SortByAge() *RepoScanner {
	// copy the pointers as fast as possible.
	things := x.selectAllRepos()

	// todo: try slices.SortFunc() instead to see what happens
	sort.Sort(sortReposAge(things))
	//    slices.SortFunc(things, func(a, b *Repos) bool {
	//        return a.Namespace < b.Namespace // Sort by ??. let the compiler work it out??
	//    })
	return newRepoScanner(things)
}

type sortReposAge []*Repo

func (a sortReposAge) Len() int { return len(a) }

// sorts in ? order
func (r sortReposAge) Less(i, j int) bool {
	if r[i].NewestAge() > r[j].NewestAge() {
		return true
	}
	return false
}
func (a sortReposAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func (x *Repos) SortActual() *Repos {
	newx := NewRepos()

	all := x.SortByFullPath()
	for all.Scan() {
		r := all.Next()
		newx.Append(r)
	}

	return newx
}