Announcement

Collapse
No announcement yet.

Using a Typed array to Sort vs Tag Arrays

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

  • Using a Typed array to Sort vs Tag Arrays

    A while back there was a discussion of Tag Arrays and sorting. I've used a Typed array to sort multiple fields with for so long I'd pretty much forgotten about Tag arrays. Anyway, here's a real life example of using a Typed Array to sort for any who may not be familiar with the practice.

    It should be relatively easy to follow (I think).

    Code:
    '*******************************************************
    ' I use the method below to sort data according to related fields.
    ' Instead of using Tag Arrays to "subsort" identical items,
    ' a Sort field in a Typed array is much easier. 
    ' Very simple, fast and easy to follow in coding
    '*******************************************************
    '
    ' Sub Sort_By is called from a CallBack function. 
    ' Sort_key is the button clicked to Call Sort_By in the Callback
    '
    ''' 'typed data used in this example set at top of program
    'Type xx03 'Check_Record
    '  SortBy As String * 60 'plenty room for sorting in this case
    '  Date As Long       
    '  Nam As String * 24 
    '  Number As Long     
    '  Amount As Currency 
    ' ... more stuff here not needed for example
    'End Type
    '   Global Checks_Display_List() As xx03 'array that holds the data
    '
    Sub Sort_by(Sort_Key As Long)
       Common_Locals 'more convenient to store frequently used Local variables than
    '
       Static Sorted_By_Last_Flag As Long 
    '
       'now assign sort -
      For ctr = LBound(Checks_Display_List()) To UBound(Checks_Display_List())                         
         Select Case Sort_Key
           '
           Case Btn_Id&(10) 'Entry order
             ' s$ less cluttered than using full array name in each statement
             s$ = Using$("######## ", Checks_Display_List(ctr).Record_Number) 
           '
           Case Btn_Id&(11) ' "Date"
             s$ = Using$("######## ", Checks_Display_List(ctr).Date) & _
                  Checks_Display_List(ctr).Nam
           '
           Case Btn_Id&(12) 'Checknumber
             s$ = Using$("######## ", Checks_Display_List(ctr).Number) & _
                  Using$("######## ", Checks_Display_List(ctr).Amount) & _
                  Checks_Display_List(ctr).Nam
           '
           Case Btn_Id&(13)'Name
             s$ = Checks_Display_List(ctr).Nam & _
                  Using$("######## ", Checks_Display_List(ctr).Date)  & _
                  Using$("######## ", Checks_Display_List(ctr).Amount)
           Case Btn_Id&(14)'Amount
             s$ = Using$("######## ", Checks_Display_List(ctr).Amount) & _
                  Checks_Display_List(ctr).Nam
           '
           Case Btn_Id&(15)'"Category"
             s$ = Checks_Display_List(ctr).Category & _
                  Checks_Display_List(ctr).Nam & _
                  Using$("######## ", Checks_Display_List(ctr).Amount)
           '
           Case Btn_Id&(16)'"SubCategory"
             s$ = Checks_Display_List(ctr).SubCategory & _
                  Checks_Display_List(ctr).Nam & _
                  Using$("######## ", Checks_Display_List(ctr).Amount)
           '
           Case Btn_Id&(17): '"Reason"
             s$ = Checks_Display_List(ctr).Reason & _
                  Using$("######## ", Checks_Display_List(ctr).Amount)
           '
         End Select 
         Checks_Display_List(ctr).SortBy = s$ 'fields to sort by inserted 
      Next ctr             
       'reverse sort if same sort button clicked again
      If Sorted_By_Last_Flag = Sort_Key Then 
         Array Sort Checks_Display_List(), Descend
         Reset Sorted_By_Last_Flag 'so as to be able to reverse again
        Else
         Array Sort Checks_Display_List() 'Ascending order
         Sorted_By_Last_Flag = Sort_Key   'set so to reverse if same sort button is clicked
      End If
    ' .. lots more code here not necessary for the example
    Hope this clears any confusion any may have about sorting multiple fields of info.

    ===================================================
    "I'm not going to get into the ring with Tolstoy."
    Ernest Hemingway (1899-1961)
    ===================================================
    Last edited by Gösta H. Lovgren-2; 1 Apr 2009, 07:05 AM. Reason: More clarity unpon the light of day
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    A long time ago I wrote a multi-key sort for PB/DOS using the ARRAY SORT "start element" and "FOR" options. I can probably find that code. (It should still be in the IMS archives.. it's public domain).

    What happened was as Gosta shows above... Windows happened. With all the extra memory available, adding an explicit 'sort key' member to a record (UDT) like this cost far less than the time necessary to effect a multi-key sort performed by actually sorting multiple keys.

    FWIW, you 'could' maintain multiple sort keys in your UDT... for alpha fields of course the sort key is just the field... and easily sort your array by your choice of keys using MACROs as demonstrated here:

    CC3+/Win7+: ARRAY SORT UDT array on member name September 04, 2002

    That is, your UDT could look like
    Code:
    TYPE Foobar 
      Key1   AS STRING * 32
      Key2   AS STRING * 8 
      CustName AS  STRING * 32 
      .... other members
    END TYPE 
    
    ... 
      LOCAL fb() AS FooBar 
      FIll fb(), building the 'key' members  
    
    ' sort by key 1: 
           array_sort_udt_member(fb,Key1,ASCEND)
    ... 
    
    ' now sort by key 2 descending: 
           array_sort_udt_member(fb,Key2,DESCEND)
    
    ' now sort by the customer name: 
           array_sort_udt_member(fb,custName,ASCEND)
    Or you could just purchase a PowerTree license.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]ems.com
    http://www.talsystems.com

    Comment


    • #3
      It should be relatively easy to follow (I think).
      Yes, kewl way to sort! One quick suggestion: it's probably even easier to follow--and faster as a bonus--to convert the numerics to strings directly. e.g.:
      Code:
      Using$("######## ", Checks_Display_List(ctr).Amount)
      
      'becomes
      
      MKCUR$(Checks_Display_List(ctr).Amount))

      Comment


      • #4
        MKCUR$() will not alpha sort in "value" order.

        None of the "MKxxx$()" functions will.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          MKCUR$() will not alpha sort in "value" order.
          Yes, my mistake. USING$("###...") is the way to go.

          Comment


          • #6
            Have you looked at the new ARRAY SORT... USING feature? It allows you to write your own comparison function, so you can not only perform multi-key sorts -- without any data-format conversions whatsoever -- you can also do complex things like "sort by descending Date, and ascending Last Name within each date".

            -- Eric
            "Not my circus, not my monkeys."

            Comment


            • #7
              Have you looked at the new ARRAY SORT... USING feature?
              I have, and it works fine just as expected. Like you've pointed out, it can do virtually any sort regardless of complexity. Many times tho only a simpler sort is needed by a number of fields all e.g. ascending, and you can save significant tix using the above sort technique.

              Comment


              • #8
                > USING$("###...") is the way to go.

                In Ye Olden Days, when the men were men, the women were grateful and memory was scarce, you could create a 'string sortable' form of numbers which saved a lot of space....

                String Signed Binary (SSB) Functions for PB-DOS

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X