1// run 2 3// Copyright 2022 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7package main 8 9import ( 10 "log" 11 "reflect" 12 "sort" 13) 14 15func main() { 16 const length = 257 17 x := make([]int64, length) 18 for i := 0; i < length; i++ { 19 x[i] = int64(i) * 27644437 % int64(length) 20 } 21 22 isLessStatic := func(i, j int) bool { 23 return x[i] < x[j] 24 } 25 26 isLessReflect := reflect.MakeFunc(reflect.TypeOf(isLessStatic), func(args []reflect.Value) []reflect.Value { 27 i := args[0].Int() 28 j := args[1].Int() 29 b := x[i] < x[j] 30 return []reflect.Value{reflect.ValueOf(b)} 31 }).Interface().(func(i, j int) bool) 32 33 sort.SliceStable(x, isLessReflect) 34 35 for i := 0; i < length-1; i++ { 36 if x[i] >= x[i+1] { 37 log.Fatalf("not sorted! (length=%v, idx=%v)\n%v\n", length, i, x) 38 } 39 } 40} 41