Results 1 to 9 of 9

Thread: I need to sort an arra on three dimensions.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    97

    I need to sort an arra on three dimensions.

    The array look like this
    ' avItem(ROW-Index, Column-Data)
    ' Column-Data: lRowIndex, ColData1, ColData2, ColData3
    avItem(1 to 100, 0 To 3)

    lRowIndex must be included in ColumnData for reference but not sorted on.

    Sort must sort all three specified columns with priority in order of ColData1, ColdData2, ColData2.

    Ascending and Descending sort would be helpful.

    Thank you.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,266

    Re: I need to sort an arra on three dimensions.

    Code:
    avItem(1 to 100, 0 To 3)
    That's only a two-dimensional array.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    97

    Re: I need to sort an arra on three dimensions.

    Not that !
    I want the data in ColData1, ColData2, ColData3 sorted with the priority order of ColData1, ColData2, ColData3.
    With lRowIdz carried along to show where then came from.

    so if ColData contained

    3, Joe, Tall, Married
    4, Dave, Tall, Single
    5, Dave, Tall, Married
    1, Joe, Tall, Single
    2, Joe, Short, Single

    I would get
    5, Dave, Tall, Married
    4, Dave, Tall, Single
    3, Joe, Tall, Married
    1, Joe, Tall, Single
    2, Joe, Short, Single

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,266

    Re: I need to sort an arra on three dimensions.

    If they're strings:

    1. Find the max length of each string in that dimension (three max lengths).
    2. Pad the strings in each dimension to make them all the same length.
    3. Make a new string array which is those three strings concatenated.
    4. Sort them.
    5. With your known lengths, parse the new array and stuff it back into the three-string old array.

    If you need to, make a UDT with your number and a single string (for the three concatenations) so you can keep the associated number with the strings.

    Another alternative is to just Format$() it, and concatenate it onto the end as well, so it can be recovered.

    Also, the way you describe this, it sounds more like a single-dimension-UDT-array with three strings, rather than a 3-dimensional-array, but hey ho.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    97

    Re: I need to sort an arra on three dimensions.

    That can take up lots of memory is an already very large app.
    The rows are long from a ListView Report and I need to keep track of the source row to later rebuild the sorted listview.

    I currently have code to do a quicksort on two keys that seems to work, however, I really need three keys.
    I have incorporated this into my ListView routines so I can sort a listview on two user selected columns. with priority based on the order selected by the user.

    If only now I could do three columns.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    97

    Re: I need to sort an arra on three dimensions.

    P.S. I have used a method exactly as you suggested using spacing in my early coding days and for this app I need a QuickSort type of in QuickSort method sorting.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,266

    Re: I need to sort an arra on three dimensions.

    Quote Originally Posted by LorinM View Post
    That can take up lots of memory is an already very large app.
    If it's taking enough memory such that you need to be worrying about it, then you should be using some kind of a database with a compound index, which can trivially do this.

    And, what do you mean by a "very large app"? Lots of code? Lots of data held in memory? If you mean "lots of code", I suspect your app isn't anywhere near as large as you suggest, and you have nothing to worry about.

    If you mean "lots of data", then you need to figure out how close you may be getting to, let's say, 1GB of data in memory. Anything approaching that, again, should be stored in a database, with I/O handled one-record-at-a-time. This is why we have databases and what they're for.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,266

    Re: I need to sort an arra on three dimensions.

    Quote Originally Posted by LorinM View Post
    P.S. I have used a method exactly as you suggested using spacing in my early coding days and for this app I need a QuickSort type of in QuickSort method sorting.
    Did you use the search feature of these forums at all? Or even try Google with site:vbforums.com specified?

    If you did, you would have easily found this and this.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,060

    Re: I need to sort an arra on three dimensions.

    If you want to sort on multiple dimensions in an array then you only have to adapt the compare method in the sort algorithm.
    All code is untested and air code


    In a normal array:
    Code:
    If myData(Index) > myData(SortIndex) Then
    Replace the line with
    Code:
    If SortCompare(myData, Index, SortIndex)  = 1 Then
    A basic single dimension SortCompare routine
    Code:
    Function SortCompare(theArray() As String, Index As Long, SortIndex As Long) As Integer
      SortCompare = StrComp(theArray(Index), theArray(SortIndex))
    End Function
    A fixed dimension SortCompare routine on 4 dimensions
    Code:
    Function SortCompare4Dimensions(theArray() As String, Index As Long, SortIndex As Long) As Integer
      Dim lDim As Long
      Dim lResult As Long 
    
      For lDim  = 0 To 3
        lResult = SortCompareDim(theArray, lDim, Index, SortIndex)
        If lResult <> 0 Then Exit For
      Next lDim
      
      SortCompare4Dimensions = lResult
    End Function
    
    Function SortCompareDim(theArray() As String, Dimension As Long, Index As Long, SortIndex As Long) As Integer
      SortCompareDim = StrComp(theArray(Index, Dimension), theArray(SortIndex, Dimension))
    End Function

Posting Permissions

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



Click Here to Expand Forum to Full Width