Skip to content

Instantly share code, notes, and snippets.

@cesarsouza
Created May 9, 2015 12:01
Show Gist options
  • Save cesarsouza/aeb3c080c502ea5702b5 to your computer and use it in GitHub Desktop.
Save cesarsouza/aeb3c080c502ea5702b5 to your computer and use it in GitHub Desktop.
Missing Sort<T> method for Jagged matrices
/// <summary>
/// Sorts the columns of a matrix by sorting keys.
/// </summary>
///
/// <param name="keys">The key value for each column.</param>
/// <param name="values">The matrix to be sorted.</param>
/// <param name="comparer">The comparer to use.</param>
///
public static TValue[][] Sort<TKey, TValue>(TKey[] keys, TValue[][] values, IComparer<TKey> comparer)
{
int[] indices = new int[keys.Length];
for (int i = 0; i < keys.Length; i++)
indices[i] = i;
Array.Sort<TKey, int>(keys, indices, comparer);
return values.Submatrix(0, values.Length - 1, indices);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment