Sorting an array

  • Thread starter Thread starter Roy Gourgi
  • Start date Start date
Yes I did, thanks it works perfectly for my needs. It's a helluva lot easier
than C++. :)

Roy
 
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?

Thanks
Roy
 
I thought that might be it, but I just wanted to make sure.

Thanks
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.
 
Array.Sort() - This method has many overloads. See which one is suitable for
you.

Hi,

How would I sort an array?

TIA
Roy
 
The simplest way is with Array.Sort(), note this example:

int[] arr = new int[]{1,5,2,6,10,3};

Array.Sort(arr);

foreach(int i in arr)
{
Console.WriteLine(i.ToString());
}

Brendan
 
Brendan Grant said:
Array.Sort(arr);
foreach(int i in arr)

Is "foreach" guaranteed to return array elements in index order? I've
never been quite sure.

P.
 
Paul E Collins said:
Is "foreach" guaranteed to return array elements in index order? I've
never been quite sure.

Yes. It would be plain silly for it to do anything else :)

It's slightly more complex in the case of multidimensional
(rectangular) arrays - I can never remember which order it enumerates
it in, but again the order is always the same. (I think it goes
[0,0], [0,1], [0,2] ... [1,0], [1,1] etc, but it's easy enough to check
if you're bothered.)
 
Is "foreach" guaranteed to return array elements in index order? I've
never been quite sure.

P.

From the definition of GetEnumerator:

"Initially, the enumerator is positioned before the first element in
the collection. .... After the end of the collection is passed, the
enumerator is positioned after the last element in the collection,"

Array and ArrayList both are ordered collections so you should be safe
to presume that using foreach goes from first to last element.
 
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.
 
Back
Top