简体   繁体   English

VB.NET基于排序对Excel列进行排名

[英]VB.NET ranking Excel columns based on a sort

Okay, I fought through a good amount of this and have gotten to the point where I can sort my arrays. 好的,我已经做了很多工作,并且可以对数组进行排序。 What I need to do now is take the following code and sort the elements in descending order instead of ascending order. 我现在需要做的是采用以下代码,并按降序而不是升序对元素进行排序。 I don't know how to do that with the two arguments in the code: 我不知道如何使用代码中的两个参数来做到这一点:

Dim Cols(3) As Int16
Cols(0) = ColumnNumber + 1
Cols(1) = ColumnNumber + 2
Cols(2) = ColumnNumber + 3
Cols(3) = ColumnNumber + 4

Dim Vals(3) As Double
Vals(0) = xlsWorkSheet.Cells(r, ColumnNumber + 1).value
Vals(1) = xlsWorkSheet.Cells(r, ColumnNumber + 2).value
Vals(2) = xlsWorkSheet.Cells(r, ColumnNumber + 3).value
Vals(3) = xlsWorkSheet.Cells(r, ColumnNumber + 4).value

Array.Sort(Vals, Cols)

If I understand your question correctly, you can implement a descending sort order by reversing the sort results. 如果我正确理解了您的问题,则可以通过反转排序结果来实现降序排序。

You should be able to provide an IComparer instance that reverses the sort order. 您应该能够提供一个IComparer实例来颠倒排序顺序。

Public Class myReverserClass
    Implements IComparer

    ' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
    Function Compare(x As [Object], y As [Object]) As Integer _
        Implements IComparer.Compare
        Return New CaseInsensitiveComparer().Compare(y, x)
    End Function 'IComparer.Compare

End Class 'myReverserClass

Perform the reverse sort like so: 执行相反的排序,如下所示:

Dim myComparer = New myReverserClass()
Array.Sort(Vals, Cols, myComparer)

There's an example on MSDN that does this: http://msdn.microsoft.com/en-us/library/system.array.sort(v=vs.71).aspx 在MSDN上有一个执行此操作的示例: http : //msdn.microsoft.com/zh-cn/library/system.array.sort(v=vs.71).aspx

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

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