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 2829
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.Collecti ons.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.co m
"Jan Smith" <JS@nospam.co m> wrote in message
news:ZO******** *************** *******@giganew s.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(a rrClone, num) + 1;
Console.WriteLi ne(myIndex.ToSt ring() + " ");
}
--
Regards
John Timney
Microsoft MVP
"Jan Smith" <JS@nospam.co m> wrote in message
news:ZO******** *************** *******@giganew s.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(item s, index);
foreach (int idx in index)
{
Console.WriteLi ne(idx);
}
}
The result is exactly what you asked for: 3 4 1 2 5
--
HTH
Stoitcho Goutsev (100)
"John Timney ( MVP )" <ti*****@despam med.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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(a rrClone, num) + 1; Console.WriteLi ne(myIndex.ToSt ring() + " "); }
-- Regards
John Timney Microsoft MVP
"Jan Smith" <JS@nospam.co m> wrote in message news:ZO******** *************** *******@giganew s.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******** *****@TK2MSFTNG P15.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(item s, index);
foreach (int idx in index) { Console.WriteLi ne(idx); } }
The result is exactly what you asked for: 3 4 1 2 5
-- HTH Stoitcho Goutsev (100)
"John Timney ( MVP )" <ti*****@despam med.com> wrote in message news:%2******** ********@TK2MSF TNGP11.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(a rrClone, num) + 1; Console.WriteLi ne(myIndex.ToSt ring() + " "); }
-- Regards
John Timney Microsoft MVP
"Jan Smith" <JS@nospam.co m> wrote in message news:ZO******** *************** *******@giganew s.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 to stdout, the only code that does this correctly is as follows:
//output of sorted array
for(i=1; i<n+1; i++)
{
cout << "value of i is: " <<...
|
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 float elements. Thanks in
advance
|
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 let's us do not go into an
"each dot over i" clarification discussion now - however you want to
call - you call it ;-)
array contains records of...
|
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 (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
| |
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 column. Once the
array elements are split, what is the best way to sort them? Thank you.
//populate data object with data from xml file.
//Data is a...
|
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 LIST WHOSE SIZE IS I LESS THAN THE SIZE OF
ORIGINAL LIST. IF V REPEAT THIS STEP AGAIN ON D UNSORTED LIST V WILL
HAVE AN ORDERED LIST OF SIZE 2 AND...
|
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 declared an array as:
char *stringArray = {"one","two","three","a"};
When I pass the array using:
|
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 sort this array using sort command?
sort($arraytest);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |