473,387 Members | 1,485 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,387 software developers and data experts.

Sorting Jagged Array how to ?

Ok I have the following Jagged array

double[][] mData = new double[259200][];
for (int x = 0; x <= 259200-1; x++)
{
mData[x]=new double[3];
}

And I load the data in to the array :

while (SR.Peek() >=0)
{
try
{
RecordLine = SR.ReadLine();
if (RecordLine != null)
{
TempStrVal=
RecordLine.Split(',');
mData[RecNum][0]=double.Parse(TempStrVal[0]);
mData[RecNum][1]=double.Parse(TempStrVal[1]);
mData[RecNum][2]=double.Parse(TempStrVal[2]);
RecNum++;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Now I need to sort the array using the third column or element of the second
array keeoing the values that belong to that element same, like when you
have three columns in excel and select to sort by column C so all values
rearrange. Any idea How can I do that.

Thansk in advance


Nov 17 '05 #1
6 8419
Carlos <cp@swa.com> wrote:
Ok I have the following Jagged array
<snip>
Now I need to sort the array using the third column or element of the second
array keeoing the values that belong to that element same, like when you
have three columns in excel and select to sort by column C so all values
rearrange. Any idea How can I do that.


Yes - use Array.Sort, passing in an instance of a class which
implements IComparer. That class's Compare method would cast both its
parameters to a double[], and compare the third elements of those
arrays.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Do you have any sample code or site where I can check it ?

Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Carlos <cp@swa.com> wrote:
Ok I have the following Jagged array


<snip>
Now I need to sort the array using the third column or element of the
second
array keeoing the values that belong to that element same, like when you
have three columns in excel and select to sort by column C so all values
rearrange. Any idea How can I do that.


Yes - use Array.Sort, passing in an instance of a class which
implements IComparer. That class's Compare method would cast both its
parameters to a double[], and compare the third elements of those
arrays.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
public class MyArrayComparer : IComparer
{
public int Compare(object x, object y)
{
double[] arrayX = (double[])x;
double[] arrayY = (double[])y;

return arrayX[2].CompareTo(arrayY[2]);
}
}

then, in your main code:

Array.Sort(mData, new MyArrayComparer());

Nov 17 '05 #4
Thanks Bruce
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
public class MyArrayComparer : IComparer
{
public int Compare(object x, object y)
{
double[] arrayX = (double[])x;
double[] arrayY = (double[])y;

return arrayX[2].CompareTo(arrayY[2]);
}
}

then, in your main code:

Array.Sort(mData, new MyArrayComparer());

Nov 17 '05 #5
Carlos, it's really quite simple. All you need is a class implementing
IComparer. For example:

public class ArrayComparer : IComparer
{
// IComparer implementation
public int Compare(object obj1, object obj2)
{
int result;

double[] m_Temp1 = obj1 as double[];
double[] m_Temp2 = obj2 as double[];
if (m_Temp1 != null && m_Temp2 != null)
{
if (m_Temp1[2] < m_Temp2[2])
result = -1;
else if (m_Temp1[2] > m_Temp2[2])
result = 1;
else
result = 0;

return result;
}
else
{
throw new ArgumentException("Objects are not arrays of
doubles!");
}
}
}

Then you can simply use the following to sort your array:

IComparer myComparer = new ArrayComparer();
Array.Sort(mData, myComparer);

Of course, you should add some error checking to make sure the arrays have
the expected structure (and do in fact contain three elements).
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"Carlos" <cp@swa.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Do you have any sample code or site where I can check it ?

Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Carlos <cp@swa.com> wrote:
Ok I have the following Jagged array


<snip>
Now I need to sort the array using the third column or element of the
second
array keeoing the values that belong to that element same, like when you
have three columns in excel and select to sort by column C so all values
rearrange. Any idea How can I do that.


Yes - use Array.Sort, passing in an instance of a class which
implements IComparer. That class's Compare method would cast both its
parameters to a double[], and compare the third elements of those
arrays.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Nov 17 '05 #6
Thanks that helps !!!
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:Of**************@TK2MSFTNGP12.phx.gbl...
Carlos, it's really quite simple. All you need is a class implementing
IComparer. For example:

public class ArrayComparer : IComparer
{
// IComparer implementation
public int Compare(object obj1, object obj2)
{
int result;

double[] m_Temp1 = obj1 as double[];
double[] m_Temp2 = obj2 as double[];
if (m_Temp1 != null && m_Temp2 != null)
{
if (m_Temp1[2] < m_Temp2[2])
result = -1;
else if (m_Temp1[2] > m_Temp2[2])
result = 1;
else
result = 0;

return result;
}
else
{
throw new ArgumentException("Objects are not arrays of
doubles!");
}
}
}

Then you can simply use the following to sort your array:

IComparer myComparer = new ArrayComparer();
Array.Sort(mData, myComparer);

Of course, you should add some error checking to make sure the arrays have
the expected structure (and do in fact contain three elements).
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Carlos" <cp@swa.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Do you have any sample code or site where I can check it ?

Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Carlos <cp@swa.com> wrote:
Ok I have the following Jagged array

<snip>

Now I need to sort the array using the third column or element of the
second
array keeoing the values that belong to that element same, like when
you
have three columns in excel and select to sort by column C so all
values
rearrange. Any idea How can I do that.

Yes - use Array.Sort, passing in an instance of a class which
implements IComparer. That class's Compare method would cast both its
parameters to a double[], and compare the third elements of those
arrays.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small...
10
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
2
by: Carlos | last post by:
I have the following two dimension array: public double mData = new double; now I need to sort the values based on the values of third index of the second dimension, like = 11 = 22 = 444
1
by: xllx.relient.xllx | last post by:
Hi, I have two questions: 1.)Is it true that an rectangular array is really just an single dimensional array that lets itself be treated as a multi-dimensional array? For example the...
2
by: tiberiu.motoc | last post by:
Hi. I've asked this question on the MSDN forums, but I've had no replies. I'm really stuck, so I'm gonna try my luck here. I have a Java web service which contains a simple function; the...
5
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: moondog | last post by:
How do you sort a 2D array? Dim myArray(10000, 9) As String myArray contains 10000 records with 9 fields. I want to sort on the Ninth field. I want to be able to use the sort method, but...
5
by: JC | last post by:
Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer<Tinterface and have run into a real roadblack. There does not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.