A SortedView provides a mechanism to dynamically alter the order in which data appears. To this end, the SortedView provides the following additional methods:
Let's try a few simple examples of sorting a 3x3 random dataset. The first example sorts the rows in 'natural order'. That is, comparing on the first column, then the second, and finally the third. The second example sorts the rows only on the second column. The final example sorts the columns based on their magnitude, and finally the view is reset to its original state and the rows permuted once.
>>> ds = Dataset(MLab.rand(3,3)) >>> sv = SortedView(ds) >>> sv.getData() [[ 0.70310301, 0.61336708, 0.29631215,] [ 0.942316 , 0.32991922, 0.58734894,] [ 0.01950742, 0.57061231, 0.17892206,]] >>> sv.sortRowsByFunction(lambda x : x.tolist()) >>> sv.getData() [[ 0.01950742, 0.57061231, 0.17892206,] [ 0.70310301, 0.61336708, 0.29631215,] [ 0.942316 , 0.32991922, 0.58734894,]] >>> sv.sortColsByFunction(lambda x : ... Numeric.sqrt(Numeric.sum(x*x))) >>> sv.getData() [[ 0.58734894, 0.32991922, 0.942316 ,] [ 0.17892206, 0.57061231, 0.01950742,] [ 0.29631215, 0.61336708, 0.70310301,]] >>> sv.reset() >>> sv.getData() [[ 0.70310301, 0.61336708, 0.29631215,] [ 0.942316 , 0.32991922, 0.58734894,] [ 0.01950742, 0.57061231, 0.17892206,]] >>> sv.permuteRows([2,1,0]) >>> sv.getData() [[ 0.01950742, 0.57061231, 0.17892206,] [ 0.942316 , 0.32991922, 0.58734894,] [ 0.70310301, 0.61336708, 0.29631215,]]
The ability to sort views by arbitrary functions is a powerful feature whose functionality is limited only by the complexity of the sorting functions. The ability to sort based on lists and tuples, allows the functions to provide primary, secondary and tertiary keys for prioritization.