I'm looking for the best way, in term of time performance, to sort multi-dimensional list. I have a multi-dimensional list with millions rows and variable number(1-10) of columns.
List<List<int?>> multiDimensionalList = new List<List<int?>>//();
{
new List<int?> { 0 ,8 ,57 ,5},
new List<int?> { 1 ,4 ,2 ,7},
new List<int?> { 2 ,0 ,-55 ,9},
new List<int?> { 3 ,8 ,57 ,2},
new List<int?> { 4 ,2 ,-120 ,4},
new List<int?> { 5 ,3 ,57 ,5},
};
int[] orderParameters = { 2, 3, 1 };
I have to order this list using the order parameters columns in input.
List<List<int?>> sortedExample = new List<List<int?>>
{
new List<int?> {4 ,2 ,-120 ,4},
new List<int?> {2 ,0 ,-55 ,9},
new List<int?> {1 ,4 ,2 ,7},
new List<int?> {3 ,8 ,57 ,2},
new List<int?> {5 ,3 ,57 ,5},
new List<int?> {0 ,8 ,57 ,5},
};
Simple implementation using Linq:
List<List<int?>> sortedList = multiDimensionalList
.OrderBy(x => x[orderParameters[2]])
.OrderBy(y => y[orderParameters[1]])
.OrderBy(z => z[orderParameters[0]])
.ToList();
Do you know a better solution?
This task is requested frequently and it must be single thread.
Performances regard generation of sortedList from multiDimensionalList.