Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Difficulties when trying to sort a custom type array

  1. #1
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13

    Difficulties when trying to sort a custom type array

    Hi,

    A you can see in enclosed file, I try to sort an array.
    This array contains members (10 in my example but more in reality) each member is
    type 
    'members
     end type
    
    defined.

    When I try to sort the array by the first member of the structure, the program crashes (see screenshots)

    This appears either with interpreted or bundled version.

    What's wrong ?

    Regards,

    Dany
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    In general Array Sort is not able to recognize array of UDT (User Defined Type) data type.
    But it should not crash, just do a binary sort based of the binary data inside UDT element
    In your example it should sort based on binary data strings composed by 10 bytes ("sName" string) + 4 bytes (binary representation of DWord "SValue") + 4 bytes (binary representation of Long "sCount")

    I have to investigate why it crash

    Thanks for reporting
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    I found the problem and solved.

    But I do not like the solution I implemented because sorting an array of UDTs would require to:
    1. make a copy of the memory allocated by the UDT
    2. splitting into dynamic strings array
    3. sorting array
    4. putting back memory into the UDT


    I'm searching a clever solution.
    I asked into Power Basic compiler forum for some help:
    https://forum.powerbasic.com/forum/u...f-unknown-size
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13
    Quote Originally Posted by ErosOlmi View Post
    In general Array Sort is not able to recognize array of UDT (User Defined Type) data type.
    But it should not crash, just do a binary sort based of the binary data inside UDT element
    In your example it should sort based on binary data strings composed by 10 bytes ("sName" string) + 4 bytes (binary representation of DWord "SValue") + 4 bytes (binary representation of Long "sCount")

    I have to investigate why it crash

    Thanks for reporting
    Eros
    Hi Eros,

    This behaviour would fulfill my needs, because a previous check does not allow duplicate values in first field (string)

    Regards


    Dany

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Please find attached thinCore.dll
    Unzip it under the following folders:
    • \thinBasic\
    • \thinBasic\thinAir

    replacing your current version

    I've fixed array sort for user defined type arrays in the "slow and dirty" way I've described above

    Let me know if it works

    Ciao
    Eros
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13
    Quote Originally Posted by ErosOlmi View Post

    Let me know if it works

    Ciao
    Eros
    No more crash !

    It works, but :

    Array Sort symbol_prg() ' empty strings first ,  then chars in ASCII order     OK for ascending
    Array Sort symbol_prg() , Descend '  give the same result.  DESCEND  seems to be ignored ?
    
    I did not test asfiles option

    Non UDT array keeps sorted OK

    Regards,


    Dany

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Reality is that thinBasic was not sorting at all fixed buffers (strings, UDT, ...)
    So it is an area all to be constructed

    I will add missing part asap ad post new Core to test.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  8. #8
    now sorting array with a structure works without giving error in the newest thinbasic v 1.10.5
    adapted from the above example:
    we want to sort the first item in the structured array and of course we want to keep item 2 and item 3 attached to it as in the original
    here 87 is still attached to 6 and 25 as before
    sorting.PNG
    Uses "console"
    
    Dim i As Long
    
    Type template
      sName As Long
      SValue As Word
      sCount As Long
    End Type
    
    
    Dim symbol(10) As template
    
    
    symbol(1).sName  = 22     symbol(1).SValue = 1    symbol(1).sCount = Rnd(1,1000)
    symbol(2).sName = 18    symbol(2).SValue = 2    symbol(2).sCount = Rnd(1,1000)
    symbol(3).sName = 2   symbol(3).SValue = 3    symbol(3).sCount = Rnd(1,1000)
    symbol(4).sName = 6  symbol(4).SValue = 4    symbol(4).sCount = Rnd(1,1000)
    symbol(5).sName = 9    symbol(5).SValue = 5    symbol(5).sCount = Rnd(1,1000)
    symbol(6).sName = 87     symbol(6).SValue = 6    symbol(6).sCount = Rnd(1,1000)
    symbol(7).sName = 54    symbol(7).SValue = 7    symbol(7).sCount = Rnd(1,1000)
    symbol(8).sName = 33    symbol(8).SValue = 8    symbol(8).sCount = Rnd(1,1000)
    symbol(9).sName = 4    symbol(9).SValue = 9    symbol(9).sCount = Rnd(1,1000)
    symbol(10).sName = 5    symbol(10).SValue = 10  symbol(10).sCount = Rnd(1,1000)
    
    
    PrintL "============ before sorting"
    
    For i = 1 To 10
      PrintL symbol(i).sName ,"    " , symbol(i).SValue ,"    " , symbol(i).sCount 
    Next i
    
    
     Array Sort symbol, DESCEND 'ASCEND
     
    PrintL "============ after sorting"
    
    For i = 1 To 10
      PrintL symbol(i).sName ,"    " , symbol(i).SValue ,"    " , symbol(i).sCount 
    Next i
    
    
    Print $CRLF&"end."
    WaitKey (60)
    
    note that in Purebasic we can choose the item from the structure to sort and it is not obligatory to choose the first item of the structure, not sure if this is possible with thinbasic or Powerbasic
    here we sort the structures array using its second item
      Structure base
        Name.l
        value.l
      EndStructure
    
      Dim something.base(10)
      
      Debug "---------- before descending sorting  : --------"
      For i=1 To 10
      something(i)\Name.l = i
      something(i)\value = Random(100, 1)
      Debug Str(something(i)\Name.l)+" -- " + Str(something(i)\value)
      Next
    
      SortStructuredArray(something(), 0, OffsetOf(base\value), TypeOf(base\value))
    
      Debug "---------- after sorting  : --------"
      For k=1 To 10
        Debug Str(something(k)\Name.l) + " -- " + Str(something(k)\value)
      Next
    
    sortPB.PNG

    as a side note: Just now i have found that we can write commands in thinbasic separated with space and without separating it with : as usual

    Uses "Console"
    
    Printl "Hello World 1"  PrintL "Hello World 2" PrintL "Hello World 3"
    PrintL
    PrintL "Press a key to end program"
    
    '---Wait for a key press
    WaitKey
    

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Primo,

    sorting by specified UDT member would be great - and I think the syntax could be even simpler than in PureBasic, because ThinBASIC is able to determine TypeOf the element.

    Looking at your code, I think you could make your life more comfy by having routines to fill and print your data.
    See this tweaked version:
    Uses "console"
     
    Dim i As Long
     
    Type template
      ' -- Data
      sName As Long
      SValue As Word
      sCount As Long
      
      ' -- Functions
      
      ' Fills in all the values at once!
      function init(sName as long, sValue as word, sCount as long)
        me.sName = sName
        me.sValue = sValue
        me.sCount = sCount
      end function
      
      ' Represents type as string
      function to_string() as string
        return strformat$("{1}{2}{3}{4}{5}", me.sName, $TAB, me.sValue, $TAB, me.sCount)
      end function
    End Type 
     
    Dim symbol(10) As template
    
    
                  'sName, sValue, sCount
    symbol(1).init(   22,      1, Rnd(1,1000))
    symbol(2).init(   18,      2, Rnd(1,1000))
    symbol(3).init(    2,      3, Rnd(1,1000))
    symbol(4).init(    6,      4, Rnd(1,1000))
    symbol(5).init(    9,      5, Rnd(1,1000))
    symbol(6).init(   87,      6, Rnd(1,1000))
    symbol(7).init(   54,      7, Rnd(1,1000))
    symbol(8).init(   33,      8, Rnd(1,1000))
    symbol(9).init(    4,      9, Rnd(1,1000))
    symbol(10).init(   5,     10, Rnd(1,1000))
    
    
    PrintL "============ before sorting"
     
    For i = 1 To 10
      PrintL symbol(i).to_string()
    Next ' <- no need to specify variable with next
     
     
     Array Sort symbol, DESCEND 'ASCEND
      
    PrintL "============ after sorting"
     
    For i = 1 To 10
      PrintL symbol(i).to_string()
    Next
     
    printl 
    printl "end."
    
    
    WaitKey (60)
    
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  10. #10
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

Page 1 of 3 123 LastLast

Similar Threads

  1. Array Sort - once more...
    By ReneMiner in forum Suggestions/Ideas discussions
    Replies: 3
    Last Post: 20-12-2015, 13:23
  2. Array Sort- issue
    By ReneMiner in forum Core module
    Replies: 3
    Last Post: 04-11-2015, 00:41
  3. Replacing ARRAY SORT
    By MikeTrader in forum Power Basic
    Replies: 9
    Last Post: 28-08-2009, 22:57
  4. Array vs type optimization
    By MouseTrap in forum Power Basic
    Replies: 6
    Last Post: 04-03-2009, 11:05

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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