Sorting Jagged Array how to ?

C

Carlos

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
 
J

Jon Skeet [C# MVP]

Carlos said:
Ok I have the following Jagged array

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.
 
C

Carlos

Do you have any sample code or site where I can check it ?

Thanks
Jon Skeet said:
Carlos said:
Ok I have the following Jagged array

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.
 
B

Bruce Wood

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());
 
K

Kai Brinkmann [MSFT]

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 said:
Do you have any sample code or site where I can check it ?

Thanks
Jon Skeet said:
Carlos said:
Ok I have the following Jagged array

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.
 
C

Carlos

Thanks that helps !!!
Kai Brinkmann said:
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 said:
Do you have any sample code or site where I can check it ?

Thanks
Jon Skeet said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top