I do want to compare indexes. Is that something possible to do? In my real code I have other criteria to compare the slice items, but if they tie, I want to use the reverse of their original order.
I expect `return i > j` to put items with larger index numbers at the front, why that doesn't seem to be the case? On Wednesday, December 7, 2022 at 11:13:12 AM UTC+8 eric.h...@gmail.com wrote: > sort.Slice(s, func(i, j int) bool { return i > j }) > > You are comparing the indexes and not the values > > -Eric > http://www.google.com/profiles/eric.hubbard > > > On Tue, Dec 6, 2022 at 6:45 PM Andrew Harris <harr...@spu.edu> wrote: > >> Subtly: >> return s[i] > s[j] >> >> Is the right sort func >> >> I think it'd be recommended to look at the generics slices package, which >> also has a sort >> On Tuesday, December 6, 2022 at 6:39:29 PM UTC-8 hey...@gmail.com wrote: >> >>> Hi, >>> >>> I have this very simple sorting code: >>> >>> s := make([]int, 0, 100) >>> for i := 1; i <= 20; i++ { >>> s = append(s, i) >>> } >>> sort.Slice(s, func(i, j int) bool { return i > j }) >>> log.Print(s) >>> >>> I expect it to print numbers in reverse order, since items with larger >>> index numbers should be at the front. However, at lease in go1.19.3, it >>> prints >>> >>> [9 1 8 5 16 3 20 2 10 7 12 13 14 15 6 4 19 18 17 11] >>> >>> I guess I must have misunderstood how the sort package works, but >>> rereading sort's doc multiple time doesn't help answer the question. >>> >>> Could anyone shed some light? >>> >> -- >> 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...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/47074e6b-b1f2-447e-b7aa-645b9a504913n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/47074e6b-b1f2-447e-b7aa-645b9a504913n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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/94a5c85a-f81f-4d11-9aa0-0f8d85efc641n%40googlegroups.com.