By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Sorting Jagged Array how to ?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.