kargo icon indicating copy to clipboard operation
kargo copied to clipboard

chore: use slices package instead of sort package

Open krancour opened this issue 1 year ago • 5 comments

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.

krancour avatar Jul 02 '24 15:07 krancour

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

ankitsridhar16 avatar Jul 02 '24 17:07 ankitsridhar16

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.

hiddeco avatar Jul 02 '24 21:07 hiddeco

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.

nitishfy avatar Jul 09 '24 07:07 nitishfy

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.

hiddeco avatar Jul 09 '24 09:07 hiddeco

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

nitishfy avatar Jul 10 '24 13:07 nitishfy