以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  sort 排序  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=189585)

--  作者:侠客1985
--  发布时间:2023/12/10 15:38:00
--  sort 排序
Dim names() As String={"赵沿骅","赵思群","范丙刚","徐红","范瑞"}
Dim Marks() As Integer={99,90,85,89,86}
Array.sort(Marks,names)
Output.show(names(0)&"="& Marks(0))
Output.show(names(1)&"="& Marks(1))
Output.show(names(2)&"="& Marks(2))
Output.show(names(3)&"="& Marks(3))
Output.show(names(4)&"="&Marks(4))
Output.show(names.length)
范丙刚=85
范瑞=86
徐红=89
赵思群=90
赵沿骅=99
5

老师,在上述对数组的排序中默认从小到大,如果要改为从大到小可以不?要怎么更改排序的方式

--  作者:linyunu1
--  发布时间:2023/12/10 17:12:00
--  
全局代码
Public Class myReverserClass
    Implements System.Collections.IComparer
    Function Compare(x As [Object], y As [Object]) As Integer Implements System.Collections.IComparer.Compare
    Return New System.Collections.CaseInsensitiveComparer().Compare(y, x)
    End Function
End Class

使用
Dim names() As String={"赵沿骅","赵思群","范丙刚","徐红","范瑞"}
Dim Marks() As Integer={99,90,85,89,86}
Dim myComparer = New myReverserClass()
Array.Sort(Marks,names,myComparer)
Output.show(names(0)&"="& Marks(0))
Output.show(names(1)&"="& Marks(1))
Output.show(names(2)&"="& Marks(2))
Output.show(names(3)&"="& Marks(3))
Output.show(names(4)&"="&Marks(4))
Output.show(names.length)

--  作者:侠客1985
--  发布时间:2023/12/10 17:47:00
--  
老师,执行结果报错图片点击可在新窗口打开查看
--  作者:linyunu1
--  发布时间:2023/12/10 20:00:00
--  
全局代码
Public Class myReverserClass
    Implements System.Collections.IComparer
    Function Compare(x As [Object], y As [Object]) As Integer Implements System.Collections.IComparer.Compare
Return New System.Collections.CaseInsensitiveComparer().Compare(y, x)
    End Function
End Class

有复制进去吗

--  作者:有点蓝
--  发布时间:2023/12/10 21:41:00
--  
Dim names() As String = {"赵沿骅", "赵思群", "范丙刚", "徐红", "范瑞"}
Dim Marks() As Integer = {99, 90, 85, 89, 86}
Dim d As New SortedDictionary(Of Integer, String)
For i As Integer = 0 To Marks.Length - 1
    d.Add(Marks(i), names(i))
Next

For i As Integer = d.Keys.Count - 1 To 0 Step - 1
    Output.Show(d.Keys(i) & "=" & d(d.Keys(i)))
Next