2012-06-05 2 views
0

IComparer 메서드를 구현하는 데 문제가 있습니다. 기본적으로 두 개의 사용자 지정 개체의 속성을 비교하려고합니다 (속성은 정수 유형 임).사전을 SortedDictionary로 변환 한 후 사용자 지정 개체에 대한 iComparer 구현

드 (문자열, customObj)는 사전입니다 prTabIndex는

(이 모든 예제 성립) customObj의 속성이며, 형 정수 입니다 좀 더 내가 3을 제안 this 스레드를 발견 검색 한 후 List 접근법, LINQ 활용 및 C# 3.0 기능 사용 그러나, VB에있어, 그들이 최선의 접근 방식은 확실하지 않습니다. 나는 일을 생각 목록 (정렬 ...

Public m As Sub(ByRef d As Dictionary(of String, customObj)) 

    Dim sortedD As New SortedDictionary(Of String, customObj)(d, myCompare) 

End Sub 

Public Class myCompare 
    Implements IComparer 

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare 
     If TryCast(x, customObj).prTabIndex < TryCast(y, customObj).prTabIndex Then 
      Return -1 
     Else 
      Return 1 
     End If 
    End Function 
End Class 


- :

... 내 자신의 IComparer 구현 압연 :

나는 세 가지 방법을 시도했습니다 이 글은 약간 학문적이다).

Dim sortedL As List(Of KeyValuePair(Of String, customObj)) = dE.ToList 
    sortedL.Sort(Function(firstPair As KeyValuePair(Of String, customObj), nextPair As KeyValuePair(Of String, customObj)) firstPair.Value.prTabIndex.CompareTo(nextPair.Value.prTabIndex)) 


... 또는 SortedDictionary로 전환 된 직접 람다 함수를 통합 : VS2008 밑줄 것을

 Dim dESorted = From kvp As KeyValuePair(Of String, customObj) In dE.ToDictionary(Function(first As KeyValuePair(Of String, customObj), second As KeyValuePair(Of String, customObj)) first.Value.prTabIndex.CompareTo(nextPair.Value.prTabIndex)) 

참고 'dE.ToDictionary을 ...'(끝에 내 마우스를 움직이는 위치에 따라 두 개의 메시지를 제공합니다.

1) "확장 메서드에서 형식 매개 변수의 데이터 형식 'signature' 'System.Linq 이러한 argu에서 Enumerable을 추론 할 수 없다. 멘토. 명시 적으로 데이터 유형을 지정하면이 오류를 정정 할 수 있습니다. "ToDictionary"위로 마우스를 가져 가면서 보았습니다.

2) 중첩 된 함수의 대리자 '서명'과 동일한 서명이 없습니다. "ToDictionary"다음에있는 항목 위로 마우스를 가져 가면 나타납니다.

틀림없이, 나는 람다 함수를 처음 사용합니다.

1) 각 구현에서 어느 정도 벗어 났습니까?

Q2) 어느 것이 계산적으로 가장 비쌉니까? 왜?

Q3) 계산 상 가장 비싼 것은 무엇입니까? 왜? 당신이 (...의) 일반에서 IComparable을 구현하는 경우

따뜻한 안부,

-sf

답변

3

당신은 자기 캐스팅을 절약 할 수 있습니다. 두 개체가 동등한 가능성도 처리해야한다고 생각합니다.

Public Class DemoClass 
    Implements IComparable(Of DemoClass) 

    Private mstrField1 As String 
    Public Property Field1() As String 
    Get 
     Return mstrField1 
    End Get 
    Set(ByVal value As String) 
     mstrField1 = value 
    End Set 
    End Property 


    Private mstrField2 As String 
    Public Property Field2() As String 
    Get 
     Return mstrField2 
    End Get 
    Set(ByVal value As String) 
     mstrField2 = value 
    End Set 
    End Property 

    Private mstrField3 As String 
    Public Property Field3() As String 
    Get 
     Return mstrField3 
    End Get 
    Set(ByVal value As String) 
     mstrField3 = value 
    End Set 
    End Property 

    ''' <summary> 
    ''' Default sort - 1 ASC, 2 ASC, 3 ASC 
    ''' </summary> 
    ''' <param name="other"></param> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Function CompareTo(ByVal other As DemoClass) As Integer Implements System.IComparable(Of DemoClass).CompareTo 
    '-1 = less than other; 0 = same as other; +1 = greater than other' 
    Select Case Me.Field1 
     Case Is < other.Field1 : Return -1 
     Case Is > other.Field1 : Return 1 
     Case Else 'equal 
     Select Case Me.Field2 
      Case Is < other.Field2 : Return -1 
      Case Is > other.Field2 : Return 1 
      Case Else 'equal 
      Select Case Me.Field3 
       Case Is < other.Field3 : Return -1 
       Case Is > other.Field3 : Return 1 
       Case Else : Return 0 'equal 
      End Select 
     End Select 
    End Select 
    End Function 
End Class 
+0

다음 : DemoClass 개체를 List (Of DemoClass)에 추가 한 다음 목록의 .Sort 함수 (Of ...)를 사용하십시오. – SSS