akuity / kargo

Application lifecycle orchestration
https://kargo.akuity.io/
Apache License 2.0
1.55k stars 133 forks source link

chore: use slices package instead of sort package #2231

Closed krancour closed 1 month ago

krancour commented 3 months ago

Since Go 1.21, the slices package contains the preferred sorting implementations. Per Go documentations, they are both more ergonomic and more performant.

This issue calls for remediating existing sort.X() function calls by replacing them with their slices.X() equivalent.

ankitsridhar16 commented 3 months ago

I can take a look and work on this. Can you provide additional details?

hiddeco commented 3 months ago

Thanks for your interest @ankitsridhar16!

Can you provide additional details?

A lot of (if not, all) things that make use of sort functions[1], should be rewritten so they use their slices counterparts.

nitishfy commented 2 months ago

hey @hiddeco, i've tried implementing the solution for it. I'm getting stucked at how are we going to use the slices package in order to sort based on a function, like here?

We can use the slices.SortFunc() for this, however, the internal implementation of SortFunc returns an int rather than bool value. Any suggestions here? After some more research, I think this is one of the way how we can implement this but would like to know your feedback on this:

slices.SortFunc(secretsList.Items, func(a, b corev1.Secret) int {
        return cmp.Compare(a.Name, b.Name)
})

However, this does not work for sorting this piece of code.

hiddeco commented 2 months ago

As slices.SortFunc documents:

cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b.

This means you can use the Compare method on time.Time:

If t is before u, it returns -1; if t is after u, it returns +1; if they're the same, it returns 0.

nitishfy commented 2 months ago

Gotcha! Please assign this issue to me, I'll fix it.