Array.Sort question

  • Thread starter Thread starter Jan Smith
  • Start date Start date
J

Jan Smith

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.
 
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.
 
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.
 
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() + " ");
}
 
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 ) said:
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 said:
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.
 
yes, thats a nice and easy suggestion.

--
Regards

John Timney
Microsoft MVP

Stoitcho Goutsev (100) said:
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 ) said:
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 said:
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.
 
Back
Top