title: Go: Loops over sorted array, sort.Sort used as value
body: <p>How to loop over sorted array?</p>
  
  <p>I got "sort.Sort used as value" error:
  <a href="https://play.golang.org/p/HP30OyJVrz" rel="nofollow">https://play.golang.org/p/HP30OyJVrz</a></p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "sort"
  )
  
  type Person struct {
      Name string
      Age  int
  }
  
  func (p Person) String() string {
      return fmt.Sprintf("%s: %d", p.Name, p.Age)
  }
  
  // ByAge implements sort.Interface for []Person based on
  // the Age field.
  type ByAge []Person
  
  func (a ByAge) Len() int           { return len(a) }
  func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
  func (a ByAge) Less(i, j int) bool { return a[i].Age &lt; a[j].Age }
  
  func main() {
      people := []Person{
          {"Bob", 31},
          {"John", 42},
          {"Michael", 17},
          {"Jenny", 26},
      }
  
      fmt.Println(people)
      sort.Sort(ByAge(people))
      fmt.Println(people)
  
      for _, p := range sort.Sort(ByAge(people)) {
          fmt.Println(p.String())
      }
  
  }
  </code></pre>