我有这个自定义分类器:
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());
您可能想要实现的类似于: 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());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句