用对应的索引对两个数组进行排序 [英] Sort two arrays with corresponded indexes

查看:117
本文介绍了用对应的索引对两个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有两个带有双精度类型数据的数组(array1和array2).我想对array1进行升序排序,并将array2与array1索引相对应.这可能吗?以下示例,

Array1(0)= 2.30
Array1(1)= 4.20
Array1(2)= 1.90
Array1(3)= 0.20
Array1(4)= 0.88

Array2(0)= 0.19
Array2(1)= 0.002
Array2(2)= 0.20
Array2(3)= 0.45
Array2(4)= 1.8

结果数组应为

结果1(0)= 4.20
结果1(1)= 2.30
结果1(2)= 1.90
结果1(3)= 0.88
结果1(4)= 0.20

结果2(0)= 0.002
结果2(1)= 0.19
结果2(2)= 0.20
Result2(3)= 1.8
结果2(4)= 0.45

有人可以帮助我获取这些结果数组吗?我使用VB.NET2010.
谢谢

Dear All,

I have two arrays (array1 and array2)with double type data. I want to sort array1 to ascending order, and array2 with corresponding to the array1 indexes. Is this possible? Example below,

Array1(0) = 2.30
Array1(1) = 4.20
Array1(2) = 1.90
Array1(3) = 0.20
Array1(4) = 0.88

Array2(0) = 0.19
Array2(1) = 0.002
Array2(2) = 0.20
Array2(3) = 0.45
Array2(4) = 1.8

Resulted arrays should be as,

Result1(0) = 4.20
Result1(1) = 2.30
Result1(2) = 1.90
Result1(3) = 0.88
Result1(4) = 0.20

Result2(0) = 0.002
Result2(1) = 0.19
Result2(2) = 0.20
Result2(3) = 1.8
Result2(4) = 0.45

Can anybody help me to get these results arrays? I use VB.NET 2010.
Thanks

推荐答案

Array.Sort(Of TKey, TValue) Method (TKey(), TValue(), IComparer(Of TKey))在这里 http://msdn.microsoft.com/en-us/library/x8kwfbye.aspx [ ^ ]可以用于此目的.在当前情况下,由于将以Descending 顺序进行排序,因此需要IComparer,在此进行了说明
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx#Y700 [^ ]
是必需的

The Array.Sort(Of TKey, TValue) Method (TKey(), TValue(), IComparer(Of TKey)) explained here http://msdn.microsoft.com/en-us/library/x8kwfbye.aspx[^] can be used for this purpose. In the present case, as the sorting is to be done in Descending order IComparer is required, which is explained here
http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx#Y700[^]
is required

Private Sub Main()
    Dim Array1(4) As  Double
    Dim Array2(4) As Double

    Array1(0) = 2.3
    Array1(1) = 4.2
    Array1(2) = 1.9
    Array1(3) = 0.2
    Array1(4) = 0.88

    Array2(0) = 0.19
    Array2(1) = 0.002
    Array2(2) = 0.2
    Array2(3) = 0.45
    Array2(4) = 1.8

    Array.Sort(Of Double, Double)(Array1, Array2, New ItemComparer())

End Sub

'ItemComparer for sorting in the Descending order
Public Class ItemComparer
    Implements IComparer(Of Double)

    Public Function Compare(x As Double, y As Double) As Integer _
        Implements IComparer(Of Double).Compare

        Return y.CompareTo(x)
    End Function
End Class


这篇关于用对应的索引对两个数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆