By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

Array.Sort question

P: n/a
I've searched the overloads for the Array.Sort method, and I haven't found a
clear answer to my question. Maybe it's not in Array.Sort.

Here's the question:

I initialize an array X with the values 28 142 3 17 225.

I can sort this array in ascending order and it will return 3 17 28 142 225.

But I want a method that will return the sort order, not the array in sorted
order. The sort order is 3 4 1 2 5.

Is there a method that will return the sort order, not the array in sorted
order?

Thanks for any help.

Feb 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
you could probably do this with a hashtable, set the key to an
incrementing int, then set the values to whatever, then after you sort
and get the values out, get the key as well and display.

thats how I would do it.

Feb 16 '06 #2

P: n/a
Jan,

No, there is not a method that will do this. You will have to write it
up yourself.

A way of doing this is to create key/value pairs for each element in the
array (you can do this with the generic KeyValuePair structure in the
System.Collections.Generic namespace), with the key being the index, and the
value being the value.

Then, sort the array of KeyValuePair instances, using a Comparer
delegate instance to sort based on the values returned by the Value property
in the KeyValuePair instance. Then, in the sorted array, you can return the
keys, which will be the indexes of the original array elements.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jan Smith" <JS@nospam.com> wrote in message
news:ZO******************************@giganews.com ...
I've searched the overloads for the Array.Sort method, and I haven't found
a
clear answer to my question. Maybe it's not in Array.Sort.

Here's the question:

I initialize an array X with the values 28 142 3 17 225.

I can sort this array in ascending order and it will return 3 17 28 142
225.

But I want a method that will return the sort order, not the array in
sorted
order. The sort order is 3 4 1 2 5.

Is there a method that will return the sort order, not the array in sorted
order?

Thanks for any help.

Feb 16 '06 #3

P: n/a
this should work for you, or at least give you a starter for 10

int[] arr = new int[] { 28, 142, 3, 17, 225 };
int[] arrClone = (int[])arr.Clone();
Array.Sort(arr);
foreach (int num in arr)
{
int myIndex = Array.IndexOf(arrClone, num) + 1;
Console.WriteLine(myIndex.ToString() + " ");
}
--
Regards

John Timney
Microsoft MVP

"Jan Smith" <JS@nospam.com> wrote in message
news:ZO******************************@giganews.com ...
I've searched the overloads for the Array.Sort method, and I haven't found
a
clear answer to my question. Maybe it's not in Array.Sort.

Here's the question:

I initialize an array X with the values 28 142 3 17 225.

I can sort this array in ascending order and it will return 3 17 28 142
225.

But I want a method that will return the sort order, not the array in
sorted
order. The sort order is 3 4 1 2 5.

Is there a method that will return the sort order, not the array in sorted
order?

Thanks for any help.

Feb 16 '06 #4

P: n/a
John,

This will work only as long as the array items are unique (no repetition).

My suggestion is to use 2 arrays: the first are the items and the second is
array of indices. For each item in the first array there is corresponding
item (at the same index) in the second array the holds the original index of
the element. The you need to apply to the second array the same
transformations as you do to the first one. The Array class provides
overload of the Sort method that does exactly this.

static void Main(string[] args)
{
int[] items = new int[] { 28, 142, 3, 17, 225 };
int[] index = new int[] { 1, 2, 3, 4, 5};

Array.Sort(items, index);

foreach (int idx in index)
{
Console.WriteLine(idx);
}
}

The result is exactly what you asked for: 3 4 1 2 5
--
HTH
Stoitcho Goutsev (100)

"John Timney ( MVP )" <ti*****@despammed.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
this should work for you, or at least give you a starter for 10

int[] arr = new int[] { 28, 142, 3, 17, 225 };
int[] arrClone = (int[])arr.Clone();
Array.Sort(arr);
foreach (int num in arr)
{
int myIndex = Array.IndexOf(arrClone, num) + 1;
Console.WriteLine(myIndex.ToString() + " ");
}
--
Regards

John Timney
Microsoft MVP

"Jan Smith" <JS@nospam.com> wrote in message
news:ZO******************************@giganews.com ...
I've searched the overloads for the Array.Sort method, and I haven't
found a
clear answer to my question. Maybe it's not in Array.Sort.

Here's the question:

I initialize an array X with the values 28 142 3 17 225.

I can sort this array in ascending order and it will return 3 17 28 142
225.

But I want a method that will return the sort order, not the array in
sorted
order. The sort order is 3 4 1 2 5.

Is there a method that will return the sort order, not the array in
sorted
order?

Thanks for any help.


Feb 16 '06 #5

P: n/a
yes, thats a nice and easy suggestion.

--
Regards

John Timney
Microsoft MVP

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:Os*************@TK2MSFTNGP15.phx.gbl...
John,

This will work only as long as the array items are unique (no repetition).

My suggestion is to use 2 arrays: the first are the items and the second
is array of indices. For each item in the first array there is
corresponding item (at the same index) in the second array the holds the
original index of the element. The you need to apply to the second array
the same transformations as you do to the first one. The Array class
provides overload of the Sort method that does exactly this.

static void Main(string[] args)
{
int[] items = new int[] { 28, 142, 3, 17, 225 };
int[] index = new int[] { 1, 2, 3, 4, 5};

Array.Sort(items, index);

foreach (int idx in index)
{
Console.WriteLine(idx);
}
}

The result is exactly what you asked for: 3 4 1 2 5
--
HTH
Stoitcho Goutsev (100)

"John Timney ( MVP )" <ti*****@despammed.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
this should work for you, or at least give you a starter for 10

int[] arr = new int[] { 28, 142, 3, 17, 225 };
int[] arrClone = (int[])arr.Clone();
Array.Sort(arr);
foreach (int num in arr)
{
int myIndex = Array.IndexOf(arrClone, num) + 1;
Console.WriteLine(myIndex.ToString() + " ");
}
--
Regards

John Timney
Microsoft MVP

"Jan Smith" <JS@nospam.com> wrote in message
news:ZO******************************@giganews.com ...
I've searched the overloads for the Array.Sort method, and I haven't
found a
clear answer to my question. Maybe it's not in Array.Sort.

Here's the question:

I initialize an array X with the values 28 142 3 17 225.

I can sort this array in ascending order and it will return 3 17 28 142
225.

But I want a method that will return the sort order, not the array in
sorted
order. The sort order is 3 4 1 2 5.

Is there a method that will return the sort order, not the array in
sorted
order?

Thanks for any help.



Feb 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.