github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/sort.go (about)

     1  package main
     2  
     3  import "sort"
     4  
     5  // sort.Slice implicitly uses reflect.Swapper
     6  
     7  func strings() {
     8  	data := []string{"aaaa", "cccc", "bbb", "fff", "ggg"}
     9  	sort.Slice(data, func(i, j int) bool {
    10  		return data[i] > data[j]
    11  	})
    12  	println("strings")
    13  	for _, d := range data {
    14  		println(d)
    15  	}
    16  }
    17  
    18  func int64s() {
    19  	sd := []int64{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
    20  	sort.Slice(sd, func(i, j int) bool {
    21  		return sd[i] > sd[j]
    22  	})
    23  	println("int64s")
    24  	for _, d := range sd {
    25  		println(d)
    26  	}
    27  
    28  	ud := []uint64{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
    29  	sort.Slice(ud, func(i, j int) bool {
    30  		return ud[i] > ud[j]
    31  	})
    32  	println("uint64s")
    33  	for _, d := range ud {
    34  		println(d)
    35  	}
    36  }
    37  
    38  func int32s() {
    39  	sd := []int32{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
    40  	sort.Slice(sd, func(i, j int) bool {
    41  		return sd[i] > sd[j]
    42  	})
    43  	println("int32s")
    44  	for _, d := range sd {
    45  		println(d)
    46  	}
    47  
    48  	ud := []uint32{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
    49  	sort.Slice(ud, func(i, j int) bool {
    50  		return ud[i] > ud[j]
    51  	})
    52  	println("uint32s")
    53  	for _, d := range ud {
    54  		println(d)
    55  	}
    56  }
    57  
    58  func int16s() {
    59  	sd := []int16{1, 6, 3, 2, 1923, 123, -123, -29, 3, 0, 1}
    60  	sort.Slice(sd, func(i, j int) bool {
    61  		return sd[i] > sd[j]
    62  	})
    63  	println("int16s")
    64  	for _, d := range sd {
    65  		println(d)
    66  	}
    67  
    68  	ud := []uint16{1, 6, 3, 2, 1923, 123, 29, 3, 0, 1}
    69  	sort.Slice(ud, func(i, j int) bool {
    70  		return ud[i] > ud[j]
    71  	})
    72  	println("uint16s")
    73  	for _, d := range ud {
    74  		println(d)
    75  	}
    76  }
    77  
    78  func int8s() {
    79  	sd := []int8{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
    80  	sort.Slice(sd, func(i, j int) bool {
    81  		return sd[i] > sd[j]
    82  	})
    83  	println("int8s")
    84  	for _, d := range sd {
    85  		println(d)
    86  	}
    87  
    88  	ud := []uint8{1, 6, 3, 2, 123, 29, 3, 0, 1}
    89  	sort.Slice(ud, func(i, j int) bool {
    90  		return ud[i] > ud[j]
    91  	})
    92  	println("uint8s")
    93  	for _, d := range ud {
    94  		println(d)
    95  	}
    96  }
    97  
    98  func ints() {
    99  	sd := []int{1, 6, 3, 2, 123, -123, -29, 3, 0, 1}
   100  	sort.Slice(sd, func(i, j int) bool {
   101  		return sd[i] > sd[j]
   102  	})
   103  	println("ints")
   104  	for _, d := range sd {
   105  		println(d)
   106  	}
   107  
   108  	ud := []uint{1, 6, 3, 2, 123, 29, 3, 0, 1}
   109  	sort.Slice(ud, func(i, j int) bool {
   110  		return ud[i] > ud[j]
   111  	})
   112  	println("uints")
   113  	for _, d := range ud {
   114  		println(d)
   115  	}
   116  }
   117  
   118  func structs() {
   119  	type s struct {
   120  		name string
   121  		a    uint64
   122  		b    uint32
   123  		c    uint16
   124  		d    int
   125  		e    *struct {
   126  			aa uint16
   127  			bb int
   128  		}
   129  	}
   130  
   131  	data := []s{
   132  		{
   133  			name: "struct 1",
   134  			d:    100,
   135  			e: &struct {
   136  				aa uint16
   137  				bb int
   138  			}{aa: 123, bb: -10},
   139  		},
   140  		{
   141  			name: "struct 2",
   142  			d:    1,
   143  			e: &struct {
   144  				aa uint16
   145  				bb int
   146  			}{aa: 15, bb: 10},
   147  		},
   148  		{
   149  			name: "struct 3",
   150  			d:    10,
   151  			e: &struct {
   152  				aa uint16
   153  				bb int
   154  			}{aa: 31, bb: -1030},
   155  		},
   156  		{
   157  			name: "struct 4",
   158  			e: &struct {
   159  				aa uint16
   160  				bb int
   161  			}{},
   162  		},
   163  	}
   164  	sort.Slice(data, func(i, j int) bool {
   165  		di := data[i]
   166  		dj := data[j]
   167  		return di.d*di.e.bb > dj.d*dj.e.bb
   168  	})
   169  	println("structs")
   170  	for _, d := range data {
   171  		println(d.name)
   172  	}
   173  }
   174  
   175  func main() {
   176  	strings()
   177  	int64s()
   178  	int32s()
   179  	int16s()
   180  	int8s()
   181  	ints()
   182  	structs()
   183  }