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

Recommended Answers

All 4 Replies

It is possible. Have you already made the code to sort array1?

dear scudzilla,
Yes, I use array.sort method to sort array1.
Array.sort(array1)

Try this:

Imports System
Imports System.Linq

Module Module1

   Sub Main()
      Dim Array1(4) As Double
      Array1(0) = 2.3
      Array1(1) = 4.2
      Array1(2) = 1.9
      Array1(3) = 0.2
      Array1(4) = 0.88

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

      Dim Array3 = Array1.OrderByDescending(Function(dbl) dbl)
      Array3.ToList().ForEach(Sub(dbl) Console.WriteLine(dbl))

      Console.WriteLine("---")

      Dim Array4 =
      (
         From a1 In Array1
         Let idxA1 = Array1.ToList().IndexOf(a1)
         Select New With {.element = a1, .index = idxA1}
      ).OrderByDescending(Function(kvp) kvp.element) _
         .Select(Function(kvp) Array2(kvp.index)).ToArray()

      Array4.ToList().ForEach(Sub(dbl) Console.WriteLine(dbl))
   End Sub

End Module

Dear Thines01,
Yes, this is working fine. Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.