2016-08-18 81 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());