# NMath Stats User's Guide

2.9 Reordering DataFrames (.NET, C#, CSharp, VB, Visual Basic, F#)

The DataFrame class provides method for both sorting rows, and for arbitrarily reordering rows and columns.

Sorting Rows

The SortRows() method sorts the rows in a data frame according to a given ordered array of column indices. The first index is the primarily sort column, the second index is the secondary sort column, and so forth. For instance:

Code Example – C#

```df.SortRows( 3, 0, 1 );
```

By default, all sorting is in ascending order.

For more control, you can also pass an array of enumerated values (Ascending or Descending):

Code Example – C#

```int[] colIndices = { 3, 0, 1 };
SortingType[] sortingTypes = { SortingType.Ascending,
SortingType.Descending,
SortingType.Ascending };
df.SortRows( colIndices, sortingTypes );
```

Finally, the SortRowsByKeys() method sorts the rows in a data frame by their row keys, in the specified order:

Code Example – C#

```df.SortRowsByKeys( SortingType.Ascending );
```

NOTE—StatsSettings.Sorting specifies the default SortingType.

Permuting Rows and Columns

The PermuteColumns() and PermuteRows() methods enable you to arbitrarily reorder the columns and rows in a data frame, respectively. Each method takes an array of indices. The array must be same length as the number of columns or rows, and contain unique indices. In both cases:

Code Example – C#

```new[ permutation[i] ] = old[ i ]
```

For example, assuming df has 3 columns, this code switches the last two columns:

Code Example – C#

```df.PermuteColumns( 0, 2, 1 );
```

Assuming df has 5 rows, this code moves the second and fourth rows to the top:

Code Example – C#

```df.PermuteRows( 2, 0, 3, 1, 4 );
```

Top

Top