Sorting with Go

package main

import (

// A type with the three methods required for sorting.
type StringSlice []string

func (p StringSlice) Len() int           { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func main() {
	names := []string{"Xavier", "John", "Eve", "Adam"}

	// sort.Strings(names) has the same effect

	fmt.Printf("%v\n", names)
comments powered by Disqus