Skip to content

Commit af21f70

Browse files
committed
feat: better sorting
1 parent 769f334 commit af21f70

File tree

5 files changed

+446
-11
lines changed

5 files changed

+446
-11
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ issues:
8787
max-same-issues: 0
8888
exclude:
8989
- 'package-comments: should have a package comment'
90+
exclude-rules:
91+
- path: (.+)_test.go
92+
linters:
93+
- funlen
94+
- goconst
95+
- maintidx
96+
- dupl
9097

9198
output:
9299
show-stats: true

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ require (
66
github.com/google/go-github/v62 v62.0.0
77
github.com/jszwec/csvutil v1.10.0
88
github.com/olekukonko/tablewriter v0.0.5
9+
github.com/stretchr/testify v1.9.0
910
github.com/urfave/cli/v2 v2.27.2
1011
golang.org/x/oauth2 v0.21.0
1112
)
1213

1314
require (
1415
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
1517
github.com/google/go-querystring v1.1.0 // indirect
1618
github.com/mattn/go-runewidth v0.0.15 // indirect
19+
github.com/pmezard/go-difflib v1.0.0 // indirect
1720
github.com/rivo/uniseg v0.4.7 // indirect
1821
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1922
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
23+
gopkg.in/yaml.v3 v3.0.1 // indirect
2024
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
22
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
46
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
57
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -14,15 +16,23 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
1416
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
1517
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
1618
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1721
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
1822
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
1923
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
2024
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
2125
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
26+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
27+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2228
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
2329
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
2430
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
2531
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
2632
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
2733
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
2834
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
35+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
36+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
38+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,7 @@ func (d *Distributary) run(ctx context.Context, formatter Formatter) error {
213213
results = append(results, result)
214214
}
215215

216-
slices.SortFunc(results, func(a, b *Result) int {
217-
if a.Ahead > b.Ahead {
218-
return -1
219-
}
220-
221-
if a.Ahead < b.Ahead {
222-
return 1
223-
}
224-
225-
return 0
226-
})
216+
slices.SortFunc(results, sortResult)
227217

228218
err = formatter(d.writer, results)
229219
if err != nil {
@@ -346,3 +336,47 @@ func newGitHubClient(ctx context.Context, token string) *github.Client {
346336

347337
return github.NewClient(oauth2.NewClient(ctx, ts))
348338
}
339+
340+
func sortResult(a, b *Result) int {
341+
if a.Behind > b.Behind {
342+
return 1
343+
}
344+
345+
if a.Behind < b.Behind {
346+
return -1
347+
}
348+
349+
if a.Ahead > b.Ahead {
350+
return -1
351+
}
352+
353+
if a.Ahead < b.Ahead {
354+
return 1
355+
}
356+
357+
if a.Stars > b.Stars {
358+
return -1
359+
}
360+
361+
if a.Stars < b.Stars {
362+
return 1
363+
}
364+
365+
if a.Issues > b.Issues {
366+
return -1
367+
}
368+
369+
if a.Issues < b.Issues {
370+
return 1
371+
}
372+
373+
if a.Forks > b.Forks {
374+
return -1
375+
}
376+
377+
if a.Forks < b.Forks {
378+
return 1
379+
}
380+
381+
return 0
382+
}

0 commit comments

Comments
 (0)