Results 1 to 6 of 6

Thread: [RESOLVED] Sort UDT array

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2018
    Posts
    4

    [RESOLVED] Sort UDT array

    Can you recommend an algorithm or idea on how to sort a UDT array with a max. of several hundred elements? The array needs to be sorted on one of the values in the type, which is a long, and contains duplicates.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: [RESOLVED] Sort UDT array

    You marked the thread as resolved, is it really resolved?

    You can use any sort algoritm you like.
    I most of the times use ShellSort, for both it's simplicity and speed.
    Code:
    Option Explicit
    
    Private Type tpUDT
      lValue As Long
      iValue As Integer
      btValue As Byte
    End Type
    
    Private Sub ShellSortUDT(tUDT() As tpUDT)
      Dim lLBound As Long, lUBound As Long
      Dim lLoop As Long, lHold As Long, lHValue As Long
      Dim tTemp As tpUDT
      
      
      lLBound = LBound(tUDT)
      lUBound = UBound(tUDT)
      
      lHValue = lLBound
    
      Do
        lHValue = 3 * lHValue + 1
      Loop Until lHValue > lUBound
        
      Do
        lHValue = lHValue / 3
        
        For lLoop = lHValue + lLBound To lUBound
          tTemp = tUDT(lLoop)
          lHold = lLoop
          Do While tUDT(lHold - lHValue).lValue > tTemp.lValue
            tUDT(lHold) = tUDT(lHold - lHValue)
            lHold = lHold - lHValue
            If lHold < lHValue Then Exit Do
          Loop
          tUDT(lHold) = tTemp
        Next lLoop
      Loop Until lHValue = lLBound
    
    End Sub

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] Sort UDT array

    Brent,

    The first thing you'd need to decide is the sort-key. I'm assuming it's one of the fields in the UDT. Once that's decided, the rest is rather trivial. I've got a quick-sort-like algorithm I often use, but Arnoutdv's also looks okay at first glance.

    Once you know what you're sorting on, just modify the algorithm to deal with your UDT, and you're done. The algorithm would be specific to that UDT, but that's just the way it's going to be with UDTs.

    Good Luck,
    Elroy
    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.

  4. #4
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: [RESOLVED] Sort UDT array

    BrentM.Adams36, it's faster not to sort UDT, instead: you can prepare sorting order (stored in another array, let's say "pos()" ) using one of the field (as suggested by @Elroy) as a key for sorting.
    Next when you need to use UDT, you apply "pos()" to your UDT array, e.g.:
    Code:
    for i = 0 to Ubound(pos)
      debug.? UDT(pos(i))
    next
    Sorting example:
    Code:
        'Sorting user type array using bufer array of positions (c) Dragokas
        Dim pos() As Long, names() As String: ReDim pos(cnt), names(cnt)
        For i = 0 To cnt: pos(i) = i: names(i) = UDT(i).DisplayName: Next 'DisplayName - here is an example of the field of your UDT used as a key for sorting
        QuickSortSpecial names, pos, 0, cnt
    Helper func.:
    Code:
    Public Sub QuickSortSpecial(j() As String, k() As Long, ByVal low As Long, ByVal high As Long)
        Dim i As Long, l As Long, M As String, wsp As String
        i = low: l = high: M = j((i + l) \ 2)
        Do Until i > l: Do While j(i) < M: i = i + 1: Loop: Do While j(l) > M: l = l - 1: Loop
            If (i <= l) Then wsp = j(i): j(i) = j(l): j(l) = wsp: wsp = k(i): k(i) = k(l): k(l) = wsp: i = i + 1: l = l - 1
        Loop
        If low < l Then QuickSortSpecial j, k, low, l
        If i < high Then QuickSortSpecial j, k, i, high
    End Sub
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Sort UDT array

    To all. This may be a suspected bot. The user has posted multiple threads throughout the forum evidently. Here's a quote from on of the threads in the "Chit Chat" forum. I wouldn't waste any more effort on this thread unless the OP replies back.
    This is someone, maybe a bot, trying to build up a post count for some nefarious reason. I've already reported it to the mods.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: [RESOLVED] Sort UDT array

    Quote Originally Posted by LaVolpe View Post
    To all. This may be a suspected bot.
    Hello. What do you mean a bot? An artificial intelligence program?

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