Announcement

Collapse
No announcement yet.

UDT Array Sort Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • UDT Array Sort Problem

    Hi Everyone,
    I've got a problem with sorting an array of UDT's that consist
    of integers. When the integers are less than 255 they get sorted
    correctly, but when they get > 255 they don't sort correctly. I
    read somewhere that PB sorts UDT's like strings, so I'm assuming
    that what happens is that once I get above 255 I'm creating a nul
    that the string sorting algorithm sees as the lowest "character"
    and puts it first in the list. Just in case I'm not making any
    sense, here's an example:
    [code]
    #Compile Exe
    #Dim All
    #Register None
    #Include "Win32API.INC"

    Type TwoInts
    Int1 As Integer
    Int2 As Integer
    End Type

    Function WinMain(ByVal hCurInstance As Long, _
    ByVal hPrevInstance As Long, _
    lpszCmdLine As Asciiz Ptr, _
    ByVal nCmdShow As Long) Export As Long
    Dim IntArray(1 To 3) As TwoInts
    Dim Counter As Integer

    'Init Array:
    For Counter = 1 To 3
    IntArray(Counter).Int1 = Counter
    IntArray(Counter).Int2 = Counter
    Next Counter

    'Sort Array:
    Array Sort IntArray(), From 3 To 4

    'Display Expected Results:
    MsgBox "Element 1 =" & Str$(IntArray(1).Int1) & Chr$(13) & _
    "Element 2 =" & Str$(IntArray(2).Int1) & Chr$(13) & _
    "Element 3 =" & Str$(IntArray(3).Int1)

    'Change The Value To > &HFF and < &HFF00 So There's A Null:
    IntArray(2).Int2 = 256

    'Sort Again:
    Array Sort IntArray(), From 3 To 4

    'Now Look Where The 256 Is:
    MsgBox "Element 1 =" & Str$(IntArray(1).Int2) & Chr$(13) & _
    "Element 2 =" & Str$(IntArray(2).Int2) & Chr$(13) & _
    "Element 3 =" & Str$(IntArray(3).Int2)
    End Function
    [\CODE]
    I understand (at least I think I do) why this is happening, but
    is there someway I can keep my UDTs and still sort correctly? If
    not, I can bust em' up into three Integer arrays, but that'll be
    somewhat of a pain. I looked at the Collate String and that's
    a possibility, but I'm not sure I'd have all my bases covered.
    Thanks In Advance, folks!!!



    ------------------
    [email protected]

  • #2
    Wyman --
    Create index array
    Code:
       #Compile Exe
       #Dim All
       #Register None
       #Include "Win32API.INC"
       Type TwoInts
          Int1 As Integer
          Int2 As Integer
       End Type
       Function PbMain
          Dim IntArray(1 To 5) As TwoInts, i As Integer
          'Init Array:
          IntArray(1).Int2 = 1423
          IntArray(2).Int2 = -1500
          IntArray(3).Int2 = 783
          IntArray(4).Int2 = 25200
          IntArray(5).Int2 = -17200
          'Sort
          ReDim Idx (LBound(IntArray()) To UBound(IntArray())) As Integer
          For i = LBound(IntArray()) To UBound(IntArray())
            idx(i) = IntArray(i).Int2
          Next
          Array Sort Idx(), TagArray IntArray()
          MsgBox "Element 1 =" & Str$(IntArray(1).Int2) & Chr$(13) & _
                 "Element 2 =" & Str$(IntArray(2).Int2) & Chr$(13) & _
                 "Element 3 =" & Str$(IntArray(3).Int2) & Chr$(13) & _
                 "Element 4 =" & Str$(IntArray(4).Int2) & Chr$(13) & _
                 "Element 5 =" & Str$(IntArray(5).Int2)
       End Function

    ------------------

    Comment


    • #3
      Hi Semen!
      To make sure I understand, you're making a second Integer array
      that's equal to the field I'm sorting on. Then you sort it with
      the UDT array as a tagarray, right? That'll work. Thanks a lot
      Semen!!!

      ------------------
      [email protected]

      Comment


      • #4
        Alternately, you can store numeric values in String Signed Binary format and sort them as characters.

        There are some functions in the source code forum to do this; search on "SSB" or "String Signed Binary" and you'll find them.

        MCM
        (inventor of SSB)
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment

        Working...
        X