473,246 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 2804
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
4
by: Radhika Sambamurti | last post by:
Hi, I'm a relative newbie.... I've written a program to do a bubble sort. take in numbers and sort them. My question is: at the very end of the sort() function, when I have to output the result...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
13
by: ANSHUL | last post by:
PLEASE PROVIDE ME D SOLUTION CODE FOR DIS PROBLEM. SELECTION SORT IS BASED ON D FOLLOWING IDEA: SELECTING D LARGEST ARRAY ELEMENT AND SWAPPING IT WITH THE LAST ARRAY ELEMENT LEAVES AN UNSORTED...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
4
by: VooDoo | last post by:
Hi, I am getting confused with sorting arrays... $arraytest =array(5) { =string(2) "39" =string(2) "44" => string(2) "77" =string(3) "150" =string(3) "464" } why do i get NULL value if i try to...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.