2016-08-18 48 views
-1

我有这样的自定义分类器:排序对象不起作用

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

' 'System.Collections.Generic.IComparer ' '不是一个错误,它只是一个命名空间?你得到的实际错误是什么? '“但是这个工作:等等......”,如果你的下一行代码行得通,那么你的问题是什么? – DGibbs

+0

为什么要为字符串实现一个costum分拣机? –

+0

投票下来请评论!!!!!!! –

回答

1

你可能想实现是这样的: IComparer<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());