title: Sort indices of slice
body: <p>I want to sort the index of elements rather than sorting the slice. For <code>[]string{&quot;dog&quot;,&quot;cat&quot;,&quot;apple&quot;,&quot;bat&quot;}</code> I want to get <code>[]int{2,3,1,0}</code></p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;sort&quot;
      &quot;strings&quot;
  )
  
  func main() {
      arr := []string{&quot;dog&quot;,&quot;cat&quot;,&quot;apple&quot;,&quot;bat&quot;}
      n := len(arr)
      indices := make([]int, n)
      for i:=0;i&lt;n;i++{
          indices[i]=i
      }
      sort.Slice(indices, func(i,j int) bool {return strings.Compare(arr[i],arr[j])&lt;0})
      fmt.Println(arr, indices) // [dog cat apple bat] [2 1 0 3]
      
      sort.Slice(arr, func(i,j int) bool {return strings.Compare(arr[i],arr[j])&lt;0})
      fmt.Println(arr) //[apple bat cat dog] 
  }
  </code></pre>