简体   繁体   English

对VB.NET使用ICOMPARER

[英]using ICOMPARER for VB.NET

I have a List (of Myclass) I want to sort on a property in my class (p propriety AS String) and according to an order determined values Dim myListeValues() AS String = {"ccc", "yyy", "aaa"} I want to use the IComparer interface as: 我有一个列表(属于Myclass),我想对我的类中的一个属性进行排序(p专有AS字符串),并根据顺序确定值Dim myListeValues()AS字符串= {“ ccc”,“ yyy”,“ aaa”我想将IComparer接口用作:

Public Class MyClass 
   Public Property p As String 

i want do : 我想做:

Private Class MyComparer Implements Icomparer(of MyClass ) 
Public Function Compare(x AS MyClass ) AS Integer Implements IComparer (Of MyClass ).Compare 

          Return   ???? 


   i want this order : 1) x.p = "ccc" 
                     2) x.p = "yyy" 
                     3) x.p = "aaa"


End FunctionEnd Class


 how can I do that?

Do you know the domain of values that you want to compare? 您知道要比较的值的范围吗? That is, do you know that they will be "ccc", "yyy", and "aaa" (or maybe other values, but all possible values are known ahead of time)? 也就是说,您是否知道它们将是“ ccc”,“ yyy”和“ aaa”(或其他值,但所有可能的值都会提前知道)?

If the answer is Yes, then I would think about using a Dictionary where the keys are your known values and the values represent the order. 如果答案是肯定的,那么我会考虑使用字典,其中的键是您的已知值,而这些值表示顺序。 Then, in the Compare implementation, retrieve the value for each input string and return the result of comparing them. 然后,在Compare实现中,检索每个输入字符串的值并返回比较它们的结果。

Please excuse my poor VB, as I don't use VB.Net and I didn't want to just post a C# version. 请原谅我可怜的VB,因为我不使用VB.Net,也不想发布C#版本。 I think you can get the idea. 我认为您可以理解。 Essentially you are creating a lookup table that maps your known strings to each string's "ordinal" value (or its position within the set of all known strings). 本质上,您正在创建一个查找表,该表将您的已知字符串映射到每个字符串的“原始”值(或其在所有已知字符串集中的位置)。 During a Compare of MyClass, get the value of Prop1 (since that looks like what you want to compare) from each of the two MyClass instances that are being compared. 在比较MyClass的过程中,从要比较的两个MyClass实例的每个实例中获取Prop1的值(因为它看起来像您要比较的内容)。 Convert each String value to Integer by looking up the string in the dictionary. 通过在字典中查找字符串,将每个String值转换为Integer。 You can then subtract one Integer from the other to get the compare result. 然后,您可以从另一整数中减去一个整数,以获得比较结果。

Some things to consider: 要考虑的一些事情:

  1. Do you want your comparison to be case insensitive (ie "aaa" == "AAA"). 您是否希望比较不区分大小写(即“ aaa” ==“ AAA”)。 If so, you will probably want to use a case insensitive dictionary. 如果是这样,您可能要使用不区分大小写的字典。

  2. You can compare (case insensitive if necessary) the string properties of the two objects to be compared and, if equal, simply return 0. No need to convert to the String values to Integer first. 您可以比较(如果需要,不区分大小写)要比较的两个对象的字符串属性,如果相等,则仅返回0。无需先将String值转换为Integer。

  3. You might want to consider what happens if one (or both) of the MyClass instances being compared has a value that is not in the dictionary (if this can happen). 您可能想考虑如果正在比较的MyClass实例中的一个(或两个)实例的值不在字典中(如果可能),该怎么办。

  4. You might want some special handling if the String property of one or both objects is null or empty. 如果一个或两个对象的String属性为null或为空,则可能需要特殊处理。

Good luck! 祝好运!

Private Class MyComparer Implements IComparer(of MyClass ) 
    Private Dictionary dict = new Dictionary of (String, Integer) (System.StringComparer.OrdinalIgnoreCase)

    Private Sub New()
        dict.Add("ccc", 0)
        dict.Add("yyy", 1)
        dict.Add("aaa", 2)
    End Sub

    Public Function Compare(x AS MyClass, y AS MyClass ) AS Integer Implements IComparer (Of MyClass ).Compare

        Boolean xb = String.IsNullOrWhitespace(x.Prop1)
        Boolean yb = String.IsNullOrWhitespace(y.Prop1)

        If xb And !yb Then Return -1 'x.Prop1 null or empty, y.Prop1 has a value
        If !xb And yb Then Return 1  'x.Prop1 has a value, y.Prop1 has a value

        If String.Compare(x.Prop1, y.Prop1, StringComparison.OrdinalIgnoreCase) = 0 Then Return 0

        Integer xi = dict[x.Prop1]
        Integer yi = dict[y.Prop1]

        Return xi - yi
    End Function
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM