按 alpha 顺序排序,先是那些没有以 APPLE 开头的,然后是那些以 APPLE 开头的

Sort in alpha order by those that are not prefaced by APPLE and then those that are prefaced with APPLE

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

艺术与手工艺

文化

保龄球

科技

苹果 - 很棒

苹果 - 健康

苹果 - 兴趣

苹果 - 营养素

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

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 

通过请求使用 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))

字符串和对象类型的自定义 IComparer class。
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())