Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: Difficulties when trying to sort a custom type array

  1. #11
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,839
    Blog Entries
    3
    Rep Power
    697
    Eros,

    what do you think about possible new syntax:
    ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [BY member][, COLLATE UCASE] [, {ASCEND | DESCEND}] [, ASFILES]
    
    In case of Primo's example, he could do for example:
    ARRAY SORT symbol BY SValue, DESCEND
    
    I checked the Core SDK, thanks to ability to thinBasic_GetTokenName, it is possible to get the UDT member name, and then, based on lookup to internal structures, do the magic.
    Let me know, if okay, I would be happy to enjoy a little PB session working on this


    Petr
    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

  2. #12
    Thanks Petr and John for the examples.
    i don't know that we can add functions inside Type.. End Type . filling data in this way is concise and consumes less space and it looks neater.

  3. #13
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10
    Quote Originally Posted by John Spikowski View Post
    Have you read the request, John, and what we are talking about here?

    You always mention Script Basic, and I'm happy to compare different languages and how different languages have solved the same problem.
    But at least remain on the subject: your link is just a generic array sort and not a sort of an UDT by one of the UDT elements.

    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #14
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10
    I have to find a general way to sort any UDT by any UDT field.
    But so far I didn't find a way but ... quite close.

    In the meantime you can develop your own script function and adapt it to your needs like in the following example.
    See template_QSort function and change as needed. You just need to change 4 lines if you need to sort by another UDT field.

    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
    
    
     
    function template_QSort(byref t1() as template, firstIndex as long, lastIndex as long)
      local low         as long
      local high        as long
      local pivotValue  as dword '<---Same type as the UDT element to sort by
      local tTemp       as template
     
      low = firstIndex
      high = lastIndex
      pivotValue = T1((firstIndex + lastIndex) / 2).sValue '---Change .sValue to any UDT element
     
      Repeat
     
        While T1(low).sValue < pivotValue '---Change .sValue to any UDT element
          low += 1
        Wend
     
        While T1(high).sValue > pivotValue '---Change .sValue to any UDT element
          high -= 1
        Wend
     
        If low <= high then
          '---SWAP
            tTemp  = T1(low)
            T1(low) = T1(high)
            T1(high) = tTemp
          '---
    
    
          low += 1
          high -= 1
        End If
     
      Until low > high
     
      If firstIndex < high then
        template_QSort(T1, firstIndex, high)
      EndIf
     
      If low < lastIndex then
        template_QSort(T1, low, lastIndex)
      End If
    End function
     
    
    
    
    
       
    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
      
    '[breakpoint] Quick Sort
    template_QSort(symbol, 1, ubound(symbol))
    PrintL
    printl "After Quick Sort by .sValue"
    For i = 1 To 10
      PrintL symbol(i).to_string()
    Next
    
    
    printl "end."
     
     
    WaitKey (60)
    
    Last edited by ErosOlmi; 18-07-2018 at 15:42.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #15
    You always mention Script Basic, and I'm happy to compare different languages and how different languages have solved the same problem.
    But at least remain on the subject: your link is just a generic array sort and not a sort of an UDT by one of the UDT elements.
    Hi Eros,

    The only reason for the SB reference was to show how an array can be sorted using BASIC code rather than depending on the language.

    I will try to be more respectful of this being the thinBasic forum.
    Last edited by John Spikowski; 18-07-2018 at 16:18.
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  6. #16
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10
    It is OK John.
    Sorry but today I had a bad (working) day and I over reacted at first
    Last edited by ErosOlmi; 18-07-2018 at 17:18.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  7. #17
    Thanks Eros, impressive solution, i have replaced in template_QSort() the sValue by the third member sCount as you said, and it sorted the structured array using the third member
    Click image for larger version. 

Name:	sorting3rd.PNG 
Views:	7 
Size:	7.3 KB 
ID:	9867
    for the users: you need the latest alpha release 1.10.5.0

  8. #18
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10
    Quote Originally Posted by Petr Schreiber View Post
    Eros,

    what do you think about possible new syntax:
    ARRAY SORT ArrayVariable([StartIndex]) [FOR nElements] [BY member][, COLLATE UCASE] [, {ASCEND | DESCEND}] [, ASFILES]
    
    In case of Primo's example, he could do for example:
    ARRAY SORT symbol BY SValue, DESCEND
    
    I checked the Core SDK, thanks to ability to thinBasic_GetTokenName, it is possible to get the UDT member name, and then, based on lookup to internal structures, do the magic.
    Let me know, if okay, I would be happy to enjoy a little PB session working on this


    Petr
    thanks Petr.
    I think I have the master idea on how to do this but I need the time.

    In the meantime I've posted a possible script solution that should not be so slow even with some thousands records to sort.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #19
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    48
    Posts
    1,241
    Blog Entries
    1
    Rep Power
    136
    My two cents for this:
    I remember that i did such thing already to sort udts by their subelements when they were displayed in some table and the user clicked the header...

    It was a bit of code because for speed reasons i used a separate sorting-function for every primitive type (byte, long, dword, single, double, string etc.) to sort.
    Core-functions like UdtElement_Offset were my secret little helpers...
    Anyway there was one thing that i had overseen and it caused me Errors: if the subelement to sort was an array itself, example foo(123).x(45)...
    Dynamic subarrays were not implemented at this time but it would make a sense to sort them by countOf(foo(index).x) instead to complain an error.
    Fixed subarrays i can not imagine how to sort - - - maybe peek$ and then sort it? Does it make sense?

    Anyway, if using UI-module we could fill our data into some invisible listview-control and abuse its sorting-abilities - does it have them or do i confuse something here ? -
    just have a column for every elements index...
    maybe no PC currently but always a head full of ideas

  10. #20
    Anyway, if using UI-module we could fill our data into some invisible listview-control and abuse its sorting-abilities - does it have them or do i confuse something here ? -
    just have a column for every elements index...
    Remember the Word Count Challange from the past?

    I used the OS sort command line utility to do my sorting of lists. Cheating but it worked great.

    Welcome back to the forum!
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

Page 2 of 3 FirstFirst 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
  •