R
Roy Gourgi
Hi,
How would I sort an array?
TIA
Roy
How would I sort an array?
TIA
Roy
Jon Skeet said:Roy Gourgi said:BTW what if I wanted to sort a 2 dimensional array based on only one key
field such as in the following:
int [,] laArray = new int[6,10];
Let's say that I want to sort each row based on laArray[0], could I use
Array.Sort() in some form or other?
You should (again) use jagged arrays - that way you'd be sorting which
row should go before which. Given the way rectangular arrays are laid
out in memory, in would be very expensive to sort rows in the same way
- the whole of the row data would have to be copied each time.
Array.Sort only works on 1-dimensional arrays (including jagged arrays,
as they're arrays of arrays).
You can, however, specify an IComparer so that you can sort the arrays
based on your own comparison.
Brendan Grant said:Array.Sort(arr);
foreach(int i in arr)
Roy Gourgi said:How would I sort an array?
Paul E Collins said:Is "foreach" guaranteed to return array elements in index order? I've
never been quite sure.
Is "foreach" guaranteed to return array elements in index order? I've
never been quite sure.
P.
Roy Gourgi said:BTW what if I wanted to sort a 2 dimensional array based on only one key
field such as in the following:
int [,] laArray = new int[6,10];
Let's say that I want to sort each row based on laArray[0], could I use
Array.Sort() in some form or other?