title: golang sort slice ascending or descending
body: <p>I need to sort a slice of a type that is coming from a 3rdparty package. Based on some condition the order must be ascending or descending.</p>
  
  <p>The solution I come up with is:</p>
  
  <pre><code>type fooAscending []foo
  
  func (v fooAscending) Len() int           { return len(v) }
  func (v fooAscending) Swap(i, j int)      { v[i], v[j] = v[j], v[i] }
  func (v fooAscending) Less(i, j int) bool { return v[i].Amount &lt; v[j].Amount }
  
  type fooDescending []foo
  
  func (v fooDescending) Len() int           { return len(v) }
  func (v fooDescending) Swap(i, j int)      { v[i], v[j] = v[j], v[i] }
  func (v fooDescending) Less(i, j int) bool { return v[i].Amount &gt; v[j].Amount }
  
  if someCondition {
      sort.Sort(fooAscending(array))
  } else {
      sort.Sort(fooDescending(array))
  }
  </code></pre>
  
  <p>Is there a better way to do this. 13 lines of code for this task and most of it is duplicated, seems a bit too much.</p>