How to sort []int32

How to sort slice of 32bit integers, I know about “sort” packege’s Ints method but unfortunately it accepts only slice of ints

By wrapping in a very simple type and then implementing sort.Interface for that.


Simpler solution with sort.Slice:


Nice, I have to remember that one! Would have saved me a lot of lines in a couple of projects…

It’s actually fairly new! Since 1.8

1 Like

This works fine but a more idiomatic ( way to do it is like this:

When you coerce your slice into the type byValue and the call to the sort function looks really nice:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.