vb.net - Implementing iComparer for custom objects after converting a Dictionary to SortedDictionary -


i'm having trouble implementing icomparer method. essentially, want compare properties of 2 custom objects (the properties of type integer).

de dictionary(of string, customobj) prtabindex property of customobj , of type integer (these hold true examples)

after more searching found this thread suggested 3 things: list approach, utilizing linq, , using c# 3.0 features. however, being in vb, not sure best approach is.

i've tried 3 different ways:

...rolling own icomparer implementation:

public m sub(byref d dictionary(of string, customobj))     dim sortedd new sorteddictionary(of string, customobj)(d, mycompare)  end sub   public class mycompare      implements icomparer      public function compare(byval x object, byval y object) integer implements system.collections.icomparer.compare         if trycast(x, customobj).prtabindex < trycast(y, customobj).prtabindex             return -1         else             return 1         end if     end function end class 


...sorting list (which, think works - making thread academic).

dim sortedl list(of keyvaluepair(of string, customobj)) = de.tolist     sortedl.sort(function(firstpair keyvaluepair(of string, customobj), nextpair keyvaluepair(of string, customobj)) firstpair.value.prtabindex.compareto(nextpair.value.prtabindex)) 


...or incorporating lambda function directly in conversion sorteddictionary:

        dim desorted = kvp keyvaluepair(of string, customobj) in de.todictionary(function(first keyvaluepair(of string, customobj), second keyvaluepair(of string, customobj)) first.value.prtabindex.compareto(nextpair.value.prtabindex)) 

note vs2008 has underlined 'de.todictionary...' (to end of line) , giving me 2 messages depending on hover mouse:

1) "data type(s) of type parameter(s) in extension method 'signature' 'signature defined in 'system.linq.enumerable cannot inferred these arguments. specifying data types explicitly might correct error. seen while hovering on "todictionary".

2) nested function not have same signature delegate 'signature'. seen while hovering on after "todictionary".

admittedly, i'm new lambda functions.

q1) how far off in each of implementations?

q2) 1 computationally least expensive? why?

q3) 1 computationally expensive? why?

warm regards,

-sf

you can save self casting if implement generic icomparable(of ...). think should handle possibility 2 objects equal.

public class democlass   implements icomparable(of democlass)    private mstrfield1 string   public property field1() string           return mstrfield1     end     set(byval value string)       mstrfield1 = value     end set   end property     private mstrfield2 string   public property field2() string           return mstrfield2     end     set(byval value string)       mstrfield2 = value     end set   end property    private mstrfield3 string   public property field3() string           return mstrfield3     end     set(byval value 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 democlass) integer implements system.icomparable(of democlass).compareto     '-1 = less other; 0 = same other; +1 = greater other'     select case me.field1       case < other.field1 : return -1       case > other.field1 : return 1       case else 'equal         select case me.field2           case < other.field2 : return -1           case > other.field2 : return 1           case else 'equal             select case me.field3               case < other.field3 : return -1               case > other.field3 : return 1               case else : return 0 'equal             end select         end select     end select   end function end class 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -