I have a two-dimensional array (of Strings) which make up my data table (of rows and columns). I want to sort this array by any column. I tried to find an algorithm for doing this in C#, but have not been successful.
Any help is appreciated.
arrayscsorting
I have a two-dimensional array (of Strings) which make up my data table (of rows and columns). I want to sort this array by any column. I tried to find an algorithm for doing this in C#, but have not been successful.
Any help is appreciated.
Best Answer
Can I check - do you mean a rectangular array (
[,]
)or a jagged array ([][]
)?It is quite easy to sort a jagged array; I have a discussion on that here. Obviously in this case the
Comparison<T>
would involve a column instead of sorting by ordinal - but very similar.Sorting a rectangular array is trickier... I'd probably be tempted to copy the data out into either a rectangular array or a
List<T[]>
, and sort there, then copy back.Here's an example using a jagged array:
For working with a rectangular array... well, here is some code to swap between the two on the fly...