On Tue, Dec 6, 2022 at 10:29 PM hey...@gmail.com <hey....@gmail.com> wrote:

> > sorts defined by an ordering function purely dependent on the value of
> the element
>
> Hmm, I thought the function was agnostic to what really get compared? If
> it offers two index numbers, and the return value says the one with larger
> index number should be at the front, shouldn't the sort function simply do
> that, since during the sorting, the passed index number should be stable?
>

This is almost correct, except that the slice is sorted in-place so the
index numbers aren't actually stable.

The i and j passed into the function should be used solely to look up
elements in the array. I suspect that in most use cases, it would be more
helpful to have a function
sort.Slice[T any](x []T, less func(a, b T) bool)
i.e. the sort function would take two values read from the list instead of
two indices, to avoid exactly this sort of confusion, but A) it was written
before generics, and B) in the particular case of []struct{...}, two
structs get copied for each call to less().

-- 
Dan

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAAViQtjq6A4CmFagM2zxEkXMkrF%2BMF74jnCsnO2oafx-OVUrMQ%40mail.gmail.com.

Reply via email to