title: What is the shortest way to simply sort an array of structs by (arbitrary) field names?
body: <p>I just had a problem where I had an array of structs, e.g.</p>
  
  <pre><code>package main
  
  import "log"
  
  type Planet struct {
      Name       string  `json:"name"`
      Aphelion   float64 `json:"aphelion"`   // in million km
      Perihelion float64 `json:"perihelion"` // in million km
      Axis       int64   `json:"Axis"`       // in km
      Radius     float64 `json:"radius"`
  }
  
  func main() {
      var mars = new(Planet)
      mars.Name = "Mars"
      mars.Aphelion = 249.2
      mars.Perihelion = 206.7
      mars.Axis = 227939100
      mars.Radius = 3389.5
  
      var earth = new(Planet)
      earth.Name = "Earth"
      earth.Aphelion = 151.930
      earth.Perihelion = 147.095
      earth.Axis = 149598261
      earth.Radius = 6371.0
  
      var venus = new(Planet)
      venus.Name = "Venus"
      venus.Aphelion = 108.939
      venus.Perihelion = 107.477
      venus.Axis = 108208000
      venus.Radius = 6051.8
  
      planets := [...]Planet{*mars, *venus, *earth}
      log.Println(planets)
  }
  </code></pre>
  
  <p>Lets say you want to sort it by <code>Axis</code>. How do you do that?</p>
  
  <p>(Note: I have seen <a href="http://golang.org/pkg/sort/" rel="noreferrer">http://golang.org/pkg/sort/</a> and it seems to work, but I have to add about 20 lines just for simple sorting by a very simple key. I have a python background where it is as simple as <code>sorted(planets, key=lambda n: n.Axis)</code> - is there something similar simple in Go?)</p>