如何对包含排序数组的数组列表进行排序?

时间:2014-06-29 16:46:17

标签: .net arrays vb.net list sorting

我有一个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

3 个答案:

答案 0 :(得分:3)

如果你想要一个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代码:

我使用了你在评论中提到的网站,做了一些改动,这就是我得到的

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

答案 1 :(得分:1)

作为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

答案 2 :(得分:1)

看看合并排序。 https://en.wikipedia.org/wiki/Merge_sort有详细信息