Sort with sort.Slice in Go

Posted by yhuang
Public (Editable by Users)
Go
Edit
package main

import (
	"fmt"
	"sort"
)

func main() {

	fruit := []string{"Apple", "Banana", "Mango", "Orange"}

	// Custom sort based on the second letter of the word.
	sort.Slice(fruit, func(i, j int) bool {
		return fruit[i][1] < fruit[j][1]
	})

	fmt.Println(fruit)
}