?
Solved

array sort

Posted on 2006-04-12
7
Medium Priority
?
219 Views
Last Modified: 2010-05-01
Hi,

Two arrays called:
Dim Arr_ReqConverted() As String   <--this contains the actual values
Dim Arr_First7Highest(6) As Integer   <---this is a index value pointer to Arr_ReqConverted()

how can we re-arrange and sort the arr_first7highest(6) ? re-arrangeing arr_first7highest(6) -highest to lowest

Note:We need to lookup the real value in Arr_ReqConverted() to determine the sort order.

i.e.

Arr_ReqConverted() = 102,201,30,405,50,60,70,80,90,100
Arr_First7Highest(6)=2,4,6,1,9,5,7  
lookup element actual valueArr_ReqConverted(Arr_First7Highest(0))=30
lookup element actual valueArr_ReqConverted(Arr_First7Highest(1))=50
lookup element actual valueArr_ReqConverted(Arr_First7Highest(2))=70
lookup element actual valueArr_ReqConverted(Arr_First7Highest(3))=201
lookup element actual valueArr_ReqConverted(Arr_First7Highest(4))=100
lookup element actual valueArr_ReqConverted(Arr_First7Highest(5))=60
lookup element actual valueArr_ReqConverted(Arr_First7Highest(6)=80

desired output:
Arr_First7Highest(6)=1,9,7,6,5,4,2


currently stuck:
    Dim Best_J As Integer
    Dim J As Integer
    Dim Best_ValueConverted As Integer
    Dim Best_Value As Integer
    For i = 0 To 6 - 1
    Best_ValueConverted = Arr_ReqConverted(Arr_First7Highest(i))
    Best_J = i
        For J = i + 1 To 6
            If Arr_ReqConverted(Arr_First7Highest(J)) > Best_ValueConverted Then
                Best_ValueConverted = Arr_ReqConverted(Arr_First7Highest(J))
                Best_Value = Arr_First7Highest(J)
                Best_J = J
            End If
        Next
       
        Arr_First7Highest(Best_J) = Arr_First7Highest(i)
        Arr_First7Highest(i) = Best_Value2
       
    Next i
0
Comment
Question by:RyanBank
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:pradapkumar
ID: 16434358
It is very difficult to understand ur question. Can u elabrate the above  step by step. So, we can help u appropriately.
0
 

Author Comment

by:RyanBank
ID: 16434372
we need to sort the values in  Arr_First7Highest(6) but inorder to sort properly, we need to lookup the value in Arr_ReqConverted()

i.e.

Arr_ReqConverted() = 102,201,30,405,50,60,70,80,90,100
Arr_First7Highest(6)=2,4,6,1,9,5,7  
lookup element actual valueArr_ReqConverted(Arr_First7Highest(0))=30
lookup element actual valueArr_ReqConverted(Arr_First7Highest(1))=50
lookup element actual valueArr_ReqConverted(Arr_First7Highest(2))=70
lookup element actual valueArr_ReqConverted(Arr_First7Highest(3))=201
lookup element actual valueArr_ReqConverted(Arr_First7Highest(4))=100
lookup element actual valueArr_ReqConverted(Arr_First7Highest(5))=60
lookup element actual valueArr_ReqConverted(Arr_First7Highest(6)=80

desired output:
Arr_First7Highest(6)=1,9,7,6,5,4,2
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 2000 total points
ID: 16434399
Hi RyanBank,

Dim Arr_ReqConverted() As String   '<--this contains the actual values
Dim Arr_First7Highest() As String   '<---this is a index value pointer to Arr_ReqConverted()

Arr_ReqConverted() = Split("102, 201, 30, 405, 50, 60, 70, 80, 90, 100", ",")
Arr_First7Highest() = Split("2,4,6,1,9,5,7", ",")

Dim blnSorted As Boolean
Dim intElement As Integer
Do
    blnSorted = True
    For intElement = LBound(Arr_First7Highest) To UBound(Arr_First7Highest)
        If intElement > LBound(Arr_First7Highest) Then
            If CInt(Arr_ReqConverted(Arr_First7Highest(intElement))) > CInt(Arr_ReqConverted(Arr_First7Highest(intElement - 1))) Then
                temp = Arr_First7Highest(intElement)
                Arr_First7Highest(intElement) = Arr_First7Highest(intElement - 1)
                Arr_First7Highest(intElement - 1) = temp
                blnSorted = False
            End If
        End If
    Next
Loop Until blnSorted = True
Dim strA As String
For intElement = LBound(Arr_First7Highest) To UBound(Arr_First7Highest)
    strA = strA & Arr_ReqConverted(Arr_First7Highest(intElement)) & vbLf
Next
MsgBox Join(Arr_First7Highest, ",") & vbLf & vbLf & strA

This does the trick.

Tim Cottee
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RyanBank
ID: 16434462
TimCottee,

Outstanding, works like a charm!

What sorting algorithm is this?

Thanks.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 16434477
RyanBank,

It is a kind of bubble-sort, essentially it switches position of elements that are higher or lower. So it may take more iterations of the loop if the elements are very unsorted to start with but fewer iterations if they are well sorted. Overall it is actually a quick way to sort elements especially with larger arrays.

Tim
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 16434481
RyanBank,

What I should point out is that on each iteration it assumes to start with that no switches take place, if we have to switch then we set blnSorted to false so it goes through again. If no switches can take place it is all perfectly sorted and we can exit the loop.

Tim
0
 

Author Comment

by:RyanBank
ID: 16452654
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question