Click or drag to resize

StatSortIndex Method (DoubleMatrix, SortDirection)

Sorts the specified data in ascending or descending order. The linear indexes of the sorted entries are similarly arranged.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static SortIndexResults SortIndex(
	DoubleMatrix data,
	SortDirection sortDirection
)

Parameters

data
Type: Novacta.AnalyticsDoubleMatrix
The data.
sortDirection
Type: Novacta.AnalyticsSortDirection
A constant to specify if to sort in ascending or descending order.

Return Value

Type: SortIndexResults
The results of the sorting operation.
Exceptions
ExceptionCondition
ArgumentNullExceptiondata is null.
ArgumentExceptionsortDirection is not a field of SortDirection.
Remarks

Matrix entries are interpreted as well ordered following a column major ordering. The position of an entry in such well ordering is referred to as the linear position of that entry.

Each entry in data occupies a given linear position. When an entry is repositioned during the sorting, the corresponding linear position is similarly repositioned. Therefore, the original linear indexes are sorted according to the arrangement of the corresponding entries in the data matrix. This method returns an instance of class SortIndexResults, which exposes the sorted data matrix through property SortedData, while the correspondingly arranged linear positions can be inspected by getting property SortedIndexes.

Examples

In the following example, the entries of a data matrix are sorted. Their linear positions are arranged accordingly.

C#
using System;

namespace Novacta.Analytics.CodeExamples
{
    public class SortIndexExample0  
    {
        public void Main()
        {
            // Create a matrix.
            var data = new double[6] {
                1,  2,
                2, -3,
                3,  4,
            };
            var matrix = DoubleMatrix.Dense(3, 2, data, StorageOrder.RowMajor);
            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);

            // Sort the data in ascending order. Arrange their linear positions
            // accordingly.
            var sortIndexResults = Stat.SortIndex(matrix, SortDirection.Ascending);

            Console.WriteLine();
            Console.WriteLine("Data sorted in ascending order:");
            Console.WriteLine(sortIndexResults.SortedData);

            Console.WriteLine();
            Console.WriteLine("Data linear positions arranged accordingly:");
            Console.WriteLine(sortIndexResults.SortedIndexes);

            // SortIndex is overloaded to accept data as a read-only matrix:
            // sort in descending order using a read-only wrapper of the data matrix.
            ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly();
            var sortReadOnlyDataResults = Stat.SortIndex(readOnlyMatrix, SortDirection.Descending);

            Console.WriteLine();
            Console.WriteLine("Using read-only data. Data descending sort:");
            Console.WriteLine(sortReadOnlyDataResults.SortedData);

            Console.WriteLine();
            Console.WriteLine("Using read-only data. Data linear positions arrangement:");
            Console.WriteLine(sortReadOnlyDataResults.SortedIndexes);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
// 1                2                
// 2                -3               
// 3                4                
// 
// 
// 
// Data sorted in ascending order:
// -3               2                
// 1                3                
// 2                4                
// 
// 
// 
// Data linear positions arranged accordingly:
// 4, 0, 1, 3, 2, 5
// 
// Using read-only data. Data descending sort:
// 4                2                
// 3                1                
// 2                -3               
// 
// 
// 
// Using read-only data. Data linear positions arrangement:
// 5, 2, 1, 3, 0, 4

See Also