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
    65
    Posts
    40
    Rep Power
    5

    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 Thumbnails Attached Thumbnails Click image for larger version. 

Name:	screenshot.png 
Views:	40 
Size:	87.5 KB 
ID:	9805  
    Attached Files Attached Files

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    40
    Rep Power
    5
    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
    51
    Posts
    8,343
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  6. #6
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    40
    Rep Power
    5
    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
    51
    Posts
    8,343
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  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
    Click image for larger version. 

Name:	sorting.PNG 
Views:	9 
Size:	7.4 KB 
ID:	9865
    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
    
    Click image for larger version. 

Name:	sortPB.PNG 
Views:	6 
Size:	6.6 KB 
ID:	9866

    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
    6,839
    Blog Entries
    3
    Rep Power
    697
    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 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  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

Posting Permissions

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