【问题标题】:How to sort a List of Arrays that contains sorted Arrays?如何对包含已排序数组的数组列表进行排序?
【发布时间】:2014-06-29 16:46:17
【问题描述】:

我有一个List(Of Integer()),其中整数数组包含已排序的元素,但列表的元素未排序,例如:

MyList(0) = {1, 35, 39, 42}
MyList(1) = {1, 5, 9, 12}
MyList(2) = {6, 8, 19, 62}

如何根据数组的顺序对列表索引进行排序以产生这个?:

MyList(0) = {1, 5, 9, 12}
MyList(2) = {1, 35, 39, 42}
MyList(1) = {6, 8, 19, 62}

我试图只评估每个数组的第一个元素,但这当然不会产生预期的结果,因为没有评估其余的值:

MyList = (From arr As Integer() In MyList Order By arr.First Ascending).ToList

【问题讨论】:

    标签: .net arrays vb.net list sorting


    【解决方案1】:

    如果你想要一个 c# 答案

    class ListComparer : IComparer<List<int>>
    {
        public int Compare(List<int> x, List<int> y)
        {
            for (int i = 0; i < Math.Min(x.Count, y.Count); i++)
            {
                if (x[i] != y[i]) return x[i] - y[i];
            }
            return x.Count - y.Count;
        }
    }
    

    var newList = MyList.OrderBy(l => l, new ListComparer()).ToList();
    

    编辑:@ElektroStudios,这将是我的第一个 VB 代码:

    我使用了您在 cmets 中提到的网站,做了一些更改,这就是我得到的

    Dim MyList As New List(Of List(Of Integer))
    
    MyList.Add(New List(Of Integer) From {1, 35, 39, 42})
    MyList.Add(New List(Of Integer) From {1, 5, 9, 12})
    MyList.Add(New List(Of Integer) From {6, 8, 19, 62})
    
    
    Dim newList = MyList.OrderBy(Function(l) l, New ListComparer()).ToList()
    

    Public Class ListComparer
    Implements IComparer(Of List(Of Integer))
    
    Public Function Compare1(x As List(Of Integer), y As List(Of Integer)) As Integer Implements IComparer(Of List(Of Integer)).Compare
        For i As Integer = 0 To Math.Min(x.Count, y.Count) - 1
            If x(i) <> y(i) Then
                Return x(i) - y(i)
            End If
        Next
        Return x.Count - y.Count
    End Function
    
    End Class
    

    【讨论】:

    • 它在我的翻译Dim newList = mylist.OrderBy(Function(l As Integer) l, New ListComparer()).ToList() 中抛出了一个异常,并带有以下消息:Data type(s) of the type parameter(s) in extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of Integer(), TKey), comparer As System.Collections.Generic.IComparer(Of TKey)) As System.Linq.IOrderedEnumerable(Of Integer())' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments because they do not convert to the same type. Specifying the data type(s) explicitly might correct this error.
    • 注意:我刚刚使用converter.telerik.com 翻译了您提供给我的代码,没有任何自己的更改,谢谢。
    【解决方案2】:

    作为对 LB 答案的补充,可以创建一个更通用的数组比较器,如下所示:

    Public Class ArrayComparer(Of T As IComparable)
        Implements IComparer(Of T())
    
        Public Function Compare(x() As T, y() As T) As Integer Implements IComparer(Of T()).Compare
            Dim i, n As Integer
            For i = 0 To (Math.Min(x.Count, y.Count) - 1)
                n = x(i).CompareTo(y(i))
                If (n <> 0) Then Return n
            Next
            Return x.Length.CompareTo(y.Length)
        End Function
    
    End Class
    

    现在,给定以下列表:

    Dim list As New List(Of Integer())
    
    For i As Integer = 1 To 2
        For j As Integer = 1 To 2
            For k As Integer = 1 To 2
                For l As Integer = 1 To 2
                    list.Add(New Integer() {i, j, k, l})
                Next
                list.Add(New Integer() {i, j, k})
            Next
            list.Add(New Integer() {i, j})
        Next
        list.Add(New Integer() {i})
    Next
    

    还有以下动作:

    list = list.OrderBy((Function(a As Integer()) a), New ArrayComparer(Of Integer)).ToList()
    

    结果如下:

    1
    1,1
    1,1,1
    1,1,1,1
    1,1,1,2
    1,1,2
    1,1,2,1
    1,1,2,2
    1,2
    1,2,1
    1,2,1,1
    1,2,1,2
    1,2,2
    1,2,2,1
    1,2,2,2
    2
    2,1
    2,1,1
    2,1,1,1
    2,1,1,2
    2,1,2
    2,1,2,1
    2,1,2,2
    2,2
    2,2,1
    2,2,1,1
    2,2,1,2
    2,2,2
    2,2,2,1
    2,2,2,2
    

    如果需要,添加辅助排序表达式以包含长度。

    list = list.OrderBy((Function(a As Integer()) a), New ArrayComparer(Of Integer)).ThenBy(Function(a As Integer()) a.Length).ToList()
    

    结果:

    1
    2
    1,1
    1,2
    2,1
    2,2
    1,1,1
    1,1,2
    1,2,1
    1,2,2
    2,1,1
    2,1,2
    2,2,1
    2,2,2
    1,1,1,1
    1,1,1,2
    1,1,2,1
    1,1,2,2
    1,2,1,1
    1,2,1,2
    1,2,2,1
    1,2,2,2
    2,1,1,1
    2,1,1,2
    2,1,2,1
    2,1,2,2
    2,2,1,1
    2,2,1,2
    2,2,2,1
    2,2,2,2
    

    【讨论】:

      【解决方案3】:

      看看归并排序。 https://en.wikipedia.org/wiki/Merge_sort有详情

      【讨论】:

        猜你喜欢
        • 2018-03-15
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        • 2010-10-29
        • 2016-04-03
        • 2011-12-09
        • 2016-02-26
        • 1970-01-01
        相关资源
        最近更新 更多