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. 5 2806
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.
--
- 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.
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.
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.
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
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...
|
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...
|
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...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
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: 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...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |