Sort - emer/etable GitHub Wiki
The Table
does not directly contain any sorting methods.
Instead, you first create an IdxView of the table, and then call one of the various Sort
methods on it.
Sort by a 1D Column
ix := etable.NewIdxView(&MyTable)
ix.SortColName("Name", etable.Ascending) // etable.Ascending or etable.Descending
SortedTable := ix.NewTable() // turn an IdxView back into a new Table organized in order of indexes
LessFunc
Sort using a The standard Go sort
library uses a "less func" for sorting -- i.e., a function that returns true if one value is less than another. Here's how you can use that to sort a table:
ix := etable.NewIdxView(&MyTable)
nmcl := MyTable.ColByName("Name")
ix.Sort(func(et *Table, i, j int) bool {
return nmcl.StringVal1D(i) < nmcl.StringVal1D(j)
})
Note that the i
and j
indexes passed into your func
are already indirected through the Idx's of the IdxView
, so if you first Filter rows of the table, only those rows will be sorted..