按字母顺序排列,依次按未以APPLE开头的字母和以APPLE开头的字母

时间:2018-07-30 14:28:07

标签: c# asp.net wpf vb.net icomparer

我有一个具有以下值的网格

工艺品

文化

保龄球

科学与技术

苹果-很棒

苹果-健康

苹果-兴趣

苹果-营养素

我正在尝试使用比较器,但无法使其正常工作。请协助。

Private Function Compare (ByVal x as Object, ByVal y as Object) as Integer Implements Compare

    Dim xCell As String = CType(x, String)
    Dim yCell As String = CType(y, String)
    Dim x1, y1 As Integer

    Dim xReturnVal As String = String.Empty
    Dim xCollection As MatchCollection = Regex.Matches(xCell, "^IAPPLE\s")

    For Each m As Match In xCollection
        xReturnVal += If(m.ToString() = "", "0", "1")
    Next

    If Not xReturnVal = "" Then
        x1 = Convert.ToInt32(xReturnVal)
    End If

    Dim yReturnVal As String = String.Empty
    Dim yCollection As MatchCollection = Regex.Matches(xCell, "^(?!IAPPLE).+")

    For Each mt As Match In yCollection
        yReturnVal += If(mt.ToString() = "", "0", "1")
    Next

    If Not yReturnVal = "" Then
        y1 = Convert.ToInt32(yReturnVal)
    End If

    'Return yReturnVal.CompareTo(xReturnVal)

    Return y1.CompareTo(x1)

End Function 

2 个答案:

答案 0 :(得分:0)

通过请求一个使用Linq对值进行排序的代码段。

List<string> list = new List<string>();

list.Add("Arts and Craft");
list.Add("APPLE - Nutrients");
list.Add("Science and Technology");
list.Add("Culture");
list.Add("APPLE - Interests");
list.Add("APPLE - Awesome");
list.Add("Bowling");
list.Add("APPLE - Healthy");

var sortedList = list.Where(x => x.Substring(0, 5) != "APPLE").OrderBy(y => y)
                   .Concat(list.Where(x => x.Substring(0, 5) == "APPLE").OrderBy(y => y));

VB

使用https://www.carlosag.net/tools/codetranslator/来翻译上面的C#代码,因此它可能不是100%准确。

Dim list As List(Of String) = New List(Of String)

list.Add("Arts and Craft")
list.Add("APPLE - Nutrients")
list.Add("Science and Technology")
list.Add("Culture")
list.Add("APPLE - Interests")
list.Add("APPLE - Awesome")
list.Add("Bowling")
list.Add("APPLE - Healthy")

Dim sortedList As var = list.Where(() => {  }, (x.Substring(0, 5) <> "APPLE")).OrderBy(() => {  }, y)
                          .Concat(list.Where(() => {  }, (x.Substring(0, 5) = "APPLE")).OrderBy(() => {  }, y))

答案 1 :(得分:0)

针对字符串和对象类型的自定义IComparer类。
VB.NetC#。我不知道首选哪个。)

比较很简单:

  • 如果两个Objects | Strings包含"APPLE",它们照常排序。

  • 如果两个比较元素不包含"APPLE"

  • ,则相同
  • 如果两个元素之一包含"APPLE",它将始终丢失。

在这里,我正在使用[List<Type>].Sort(new [ComparerClass]());,但是它可以与其他方法一起使用。
LINQ的替代者:

string[] input = new string[] { "APPLE - Interests", "Arts and Craft", "Science and Technology", "Culture",
                                "APPLE - Nutrients", "APPLE - Awesome", "Bowling", "APPLE - Healthy"};

string[] SortedApples = input.OrderBy(apple => apple, new AppleComparer()).ToArray();

编辑:将Contains()更改为StartsWith()。在这里可能更好。


C# 版本

public class AppleComparer : IComparer<string>, IComparer<object>
{
    public int Compare(string x, string y)
    {
        if (!x.StartsWith("APPLE") & !y.StartsWith("APPLE")) {
            return x.CompareTo(y);
        }
        else if (x.StartsWith("APPLE") & !y.StartsWith("APPLE")) {
            return 1;
        }
        else if (!x.StartsWith("APPLE") & y.StartsWith("APPLE")) {
            return -1;
        }
        else { return x.CompareTo(y); }
    }

    public int Compare(object x, object y)
    {
        return Compare(x.ToString(), y.ToString());
    }
}

可以这样使用:

List<object> ApplesAndCo = new List<object>();
//Or
List<string> ApplesAndCo = new List<string>();
ApplesAndCo.AddRange(new[] { "APPLE - Interests", "Arts and Craft", "Science and Technology", "Culture",
                             "APPLE - Nutrients", "APPLE - Awesome", "Bowling", "APPLE - Healthy"});

ApplesAndCo.Sort(new AppleComparer());

VB.Net 版本。

Public Class AppleComparer
    Implements IComparer(Of String)
    Implements IComparer(Of Object)

    Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
        If (Not x.StartsWith("APPLE")) And (Not y.StartsWith("APPLE")) Then
            Return x.CompareTo(y)
        ElseIf x.StartsWith("APPLE") And Not y.StartsWith("APPLE") Then
            Return 1
        ElseIf Not x.StartsWith("APPLE") And y.StartsWith("APPLE") Then
            Return -1
        Else
            Return x.CompareTo(y)
        End If
    End Function
    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer(Of Object).Compare
        Return Compare(x.ToString(), y.ToString())
    End Function
End Class

VB.Net的一些东西:

Dim ApplesAndCo As List(Of Object) = New List(Of Object)()
'Or as a list og String
'Dim ApplesAndCo As List(Of String) = New List(Of String)()

ApplesAndCo.AddRange({"APPLE - Interests", "Arts and Craft", "Science and Technology", "Culture",
                      "APPLE - Nutrients", "APPLE - Awesome", "Bowling", "APPLE - Healthy"})

ApplesAndCo.Sort(New AppleComparer())