用对应的索引对两个数组进行排序

编程入门 行业动态 更新时间:2024-10-11 17:19:12
本文介绍了用对应的索引对两个数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

亲爱的所有人, 我有两个带有双精度类型数据的数组(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))在这里 msdn.microsoft/en-us/library/x8kwfbye.aspx [ ^ ]可以用于此目的.在当前情况下,由于将以Descending 顺序进行排序,因此需要IComparer,在此进行了说明 msdn.microsoft/en-us/library/8ehhxeaf.aspx#Y700 [^ ] 是必需的 The Array.Sort(Of TKey, TValue) Method (TKey(), TValue(), IComparer(Of TKey)) explained here msdn.microsoft/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 msdn.microsoft/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

更多推荐

用对应的索引对两个数组进行排序

本文发布于:2023-11-29 04:08:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!