Consulting

Results 1 to 9 of 9

Thread: Sort Arrays ...

  1. #1

    Sort Arrays ...

    Hi All,

    I have two sets of arrays with related values and different data types.
    NumArr(4) = {22,17,24,16}
    StrArr(4) = {"A","D","C","B"}
    These values are derived from the spreadsheet, where "A" has a value of 22, "B" has a value of 17 etc.
    When I want to plot these values, assigning SeriesValues to NumArr and SeriesLabels to StrArr, I get an uneven column chart. I want to display a smooth ascending column chart by sorting NumArr. But by so doing, I should ensure that the order of the string values in the StrArr is also changed accordingly, so that the original relationship holds.
    This means that NumArr(1) corresponds to StrArr(1) always and so on for other indexes.
    I thought of using a 2-dimensional array, but the data types of the two variables are different.
    Is there anyway I can do it by VBA?
    Any suggestion will be appreciated.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    perhaps

    [VBA]
    Sub sort_arr()
    'http://www.cpearson.com/excel/SortingArrays.aspx

    Dim ws As Worksheet
    Dim R As Range
    Dim Arr As Variant

    Application.ScreenUpdating = False

    Set ws = ThisWorkbook.Worksheets.Add
    Set R = ws.Range("A1").Resize(UBound(myArray) - LBound(myArray) + 1, 2)

    Arr = [{"A",22;"D",17;"C",24;"B",16}]
    R = Arr
    R.Sort key1:=R.Columns(2), order1:=xlAscending, MatchCase:=False
    Arr = R

    Application.DisplayAlerts = False
    ws.Delete
    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you can test the code with this:

    [VBA]
    Sub test()

    Dim ws
    Dim R1 As Range, R2 As Range
    Dim Arr As Variant

    Set ws = ThisWorkbook.Worksheets.Add

    Arr = [{"A",22;"D",17;"C",24;"B",16}]

    Set R1 = ws.Range("A1").Resize(UBound(Arr) - LBound(Arr) + 1, 2)
    R1 = Arr
    R1.Sort key1:=R1.Columns(2), order1:=xlAscending, MatchCase:=False
    Arr = R1

    Set R2 = ws.Range("G1").Resize(UBound(Arr) - LBound(Arr) + 1, 2)
    R2 = Arr

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Hello mancubus,

    Thanks for posting the solution. This will create a new worksheet, where the values will be pasted and sorted.

    For the specific purpose, I tried the following subroutine.
    [VBA]
    Sub SortTwoRelatedArrays(arNum() As Double, arStr() As String)
    Dim nOne As Double, nTwo As Double
    Dim strOne As String, strTwo As String
    Dim i As Integer, j As Integer
    'Check that the number of elements in both arrays are equal.
    If (UBound(arNum) <> UBound(arStr)) Then
    MsgBox ("Arrays DO NOT contain same number of elements.")
    Exit Sub
    End If

    For i = 1 To UBound(arNum)
    For j = i To UBound(arNum)
    If (arNum(j) < arNum(i)) Then
    nOne = arNum(i)
    nTwo = arNum(j)
    strOne = arStr(i)
    strTwo = arStr(j)
    arNum(i) = nTwo
    arNum(j) = nOne
    arStr(i) = strTwo
    arStr(j) = strOne
    End If
    Next j
    Next i

    End Sub
    [/VBA]

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    this works for me.

    [VBA]
    Sub arr_sort()
    'http://excelwiki.com/vba-arrays/bubble-sort-in-vba

    Dim aNum(), aStr(), Arr(), t
    Dim i As Long, j As Long, UB As Long, y As Long

    aNum = Array(22, 17, 24, 16)
    aStr = Array("A", "D", "C", "B")

    If (UBound(aNum) <> UBound(aStr)) Then
    MsgBox ("Arrays DO NOT contain same number of elements.")
    Exit Sub
    End If

    UB = UBound(aNum) - LBound(aNum) + 1

    For i = LBound(aNum) To UBound(aNum)
    j = j + 1
    ReDim Preserve Arr(1 To UB, 1 To 2)
    Arr(j, 1) = aNum(i) 'aNum : col 1
    Arr(j, 2) = aStr(i)
    Next i

    Range("A1:B4") = Arr 'to see unsorted array values

    SortColumn = 1 'aNum : col 1
    For i = LBound(Arr, 1) To UBound(Arr, 1) - 1
    For j = LBound(Arr, 1) To UBound(Arr, 1) - 1
    Condition = Arr(j, SortColumn) > Arr(j + 1, SortColumn)
    If Condition Then
    For y = LBound(Arr, 2) To UBound(Arr, 2)
    t = Arr(j, y)
    Arr(j, y) = Arr(j + 1, y)
    Arr(j + 1, y) = t
    Next y
    End If
    Next j
    Next i

    Range("A11:B14") = Arr 'to see sorted array values

    End Sub
    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Regular Chabu's Avatar
    Joined
    Dec 2010
    Location
    Brussels
    Posts
    85
    Location
    You can also use a variant array then you can put anythin in it and sort the array directly.

  7. #7
    VBAX Regular Chabu's Avatar
    Joined
    Dec 2010
    Location
    Brussels
    Posts
    85
    Location
    Theats what you did already, (need to check my eyes)

  8. #8
    Hello mancubus,

    Basically we are creating a 2-dimensional array with data types as variants. I did not think of the 'variant' data type.
    Since it is easy to understand, I thought of using the classic 'bubble sort' on one array and sort the values simultaneously in the second array.

    As my application is for sorting the values for the coordinates for a chart (small set of values), that suited me quite well.

    However, it is a good to know that there are alternatives and thank you for posting the solution.

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you're wellcome.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •