IComparer works with Arraylist but not List<xxx>

J

jleslie48

This works:


class faPair {
public decimal freq;
public decimal amp;
}//class fapair

///
/// This class will do the comparing. Change the order of
/// left and right to obtain an descending order.
///
class faPairComparer : IComparer {
#region IComparer Members

public int Compare(object x, object y) {
faPair left = (faPair)x;
faPair right = (faPair)y;
return left.freq.CompareTo(right.freq);
} //Compare

#endregion
}//class PairComparer

private faPair get_af_csv_file(string cvsfilename) {
faPair ansfaPair = new faPair();

//...

return ansfaPair;
} //get_af_csv_file

private void folderBrowserDialog1_HelpRequest(object sender,
EventArgs e)
{

ArrayList plotpt_pairs = new ArrayList();
//...
for (int filelooper=0; filelooper < files.Length;
filelooper++) {
plotpt_pairs.Add(

get_af_csv_file(files[filelooper])
);
}//for filelooper

plotpt_pairs.Sort(new faPairComparer() );
} //folderBrowserDialog1_HelpRequest



but I can't get the syntax right if I make plotpt_pairs a List
<faPair> ...

as in:
private void folderBrowserDialog1_HelpRequest(object sender,
EventArgs e)
{

List<faPair> plotpt_pairs = new List<faPair>();
//...
for (int filelooper=0; filelooper < files.Length;
filelooper++) {
plotpt_pairs.Add(

get_af_csv_file(files[filelooper])
);
}//for filelooper

plotpt_pairs.Sort(new faPairComparer() ); //<----
<<<< this syntax is no good.
} //folderBrowserDialog1_HelpRequest


Error 1 The best overloaded method match for
'System.Collections.Generic.List<WindowsFormsApplication01.faPair>.Sort(System.Comparison<WindowsFormsApplication01.faPair>)'
has some invalid arguments C:\jon\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs 257 20
WindowsFormsApplication01

Error 2 Argument 1: cannot convert from
'WindowsFormsApplication01.faPairComparer' to
'System.Comparison<WindowsFormsApplication01.faPair>' C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs 257 38
WindowsFormsApplication01


any ideas?

Tia,

jleslie48
 
J

jleslie48

jleslie48 said:
[...]
Error      1       The best overloaded method match for
'System.Collections.Generic.List<WindowsFormsApplication01.faPair>.Sort(System.Comparison<WindowsFormsApplication01.faPair>)'
has some invalid arguments C:\jon\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs      257     20
WindowsFormsApplication01
Error      2       Argument 1: cannot convert from
'WindowsFormsApplication01.faPairComparer' to
'System.Comparison<WindowsFormsApplication01.faPair>'        C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs      257     38
WindowsFormsApplication01
any ideas?

Yes.  Don't try to pass an IComparer instance to a method that requires
an IComparer<T> instance instead.

The error messages tell you almost everything you need to know.  It's a
little confusing, because overload resolution finds that the
Sort(Comparison) method is the best match (for whatever reason).  But
the error messages would be basically the same even if overload
resolution had picked Sort(IComparer<T>) instead, just with a different
type name for the method.

Pete

I sort of underdstand, thanks. by googling "IComparer<t>" I can see
that there is some
fundemental differences. The article:

http://devcenter.auburnrandall.com/Default.aspx?type=post&id=86


is a good explanation.

Thanks for pointing me in the right direction.

Sincerely,

Jon
 
J

jleslie48

jleslie48 said:
[...]
Error      1       The best overloaded method match for
'System.Collections.Generic.List<WindowsFormsApplication01.faPair>.Sort(System.Comparison<WindowsFormsApplication01.faPair>)'
has some invalid arguments C:\jon\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs     257     20
WindowsFormsApplication01
Error      2       Argument 1: cannot convert from
'WindowsFormsApplication01.faPairComparer' to
'System.Comparison<WindowsFormsApplication01.faPair>'        C:\jon
\visualstudio2010\csharp
\WindowsFormsApplication01\WindowsFormsApplication01\Form1.cs     257     38
WindowsFormsApplication01
any ideas?
Yes.  Don't try to pass an IComparer instance to a method that requires
an IComparer<T> instance instead.
The error messages tell you almost everything you need to know.  It'sa
little confusing, because overload resolution finds that the
Sort(Comparison) method is the best match (for whatever reason).  But
the error messages would be basically the same even if overload
resolution had picked Sort(IComparer<T>) instead, just with a different
type name for the method.

I sort of underdstand, thanks.  by googling "IComparer<t>"  I can see
that there is some
fundemental differences.  The article:

http://devcenter.auburnrandall.com/Default.aspx?type=post&id=86

is a good explanation.

Thanks for pointing me in the right direction.

Sincerely,

Jon

works now!


heres the syntax:

class faPair {
public decimal freq;
public decimal amp;
}//class fapair

///
/// This class will do the comparing. Change the order of
/// left and right to obtain an descending order.
///
class faPairComparer : IComparer<faPair> {

public int Compare(faPair left, faPair right) {
return left.freq.CompareTo(right.freq);
} //Compare

}//class PairComparer

....

List<faPair> plotpt_pairs = new List<faPair>();

for (int filelooper=0; filelooper < files.Length;
filelooper++) {
plotpt_pairs.Add(

get_af_csv_file(files[filelooper])
);
}//for filelooper

plotpt_pairs.Sort(new faPairComparer() );
 

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