471,305 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Array.Sort question

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
5 2663
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
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
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
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
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.

Similar topics

4 posts views Thread by its me | last post: by
4 posts views Thread by Radhika Sambamurti | last post: by
4 posts views Thread by Todd | last post: by
7 posts views Thread by ritchie | last post: by
21 posts views Thread by yeti349 | last post: by
24 posts views Thread by Michael | last post: by
4 posts views Thread by VooDoo | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.