我有这个自定义排序器:
public class AlphaNumericSorter : IComparer<string>
{
public int Compare(string x, string y)
{
return SafeNativeMethods.StrCmpLogicalW(x, y);
}
}
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
我想对所有对象进行排序,但它只是对我的一个列进行排序,我必须通过我的列。
我需要根据jointnumber
List<ViewTestPackageHistorySheet> testList = _reportTestPackageHistorySheetRepository.ShowReport(Id).ToList();
testList.Sort(new AlphaNumericSorter());
我收到了这个错误:
'System.Collections.Generic.IComparer<ViewDomainClass.Report.TestPackage.ViewTestPackageHistorySheet>'
但这有效:
List<string> testList = _reportTestPackageHistorySheetRepository.ShowReport(Id).Select(i=>i.JointNumber).ToList();
testList.Sort(new AlphaNumericSorter());
答案 0 :(得分:1)
您可能希望实现如:IComparer<ViewTestPackageHistorySheet>
您想比较ViewTestPackageHistorySheet
而不是string
类似的东西:
public class AlphaNumericSorter : IComparer<ViewTestPackageHistorySheet>
{
public int Compare(ViewTestPackageHistorySheet x, ViewTestPackageHistorySheet y)
{
return SafeNativeMethods.StrCmpLogicalW(x.JointNumber, y.JointNumber);
}
}
[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogicalW(string psz1, string psz2);
}
使用它像:
var result = _reportTestPackageHistorySheetRepository.ShowReport(Id).ToList();
result.Sort(new AlphaNumericSorter());