QSORT statement

Purpose: QSORT sorts an array (or portion). The array can be integer, floating point or string. Note that string array comparisons are case insensitive, that is, 'A' is equal to 'a'.


 Syntax 1:
 
 QSORT Array, Elements, [Order]

 Parameters:

  • Array STATIC string or numeric array. Use an identifier to indicate the data type of the array. Do not include the [ ] brackets when specifying the Array name.
  • Elements Number of elements to sort.
  • Order [OPTIONAL] ASCENDING or DESCENDING specifies sort order Default is ASCENDING order.

 Syntax 2:

 QSORT DYNAMIC Array, Elements, [Order]

 Parameters:

  • Array DYNAMIC string or numeric array. Use an identifier to indicate the data type of the array. Do not include the [ ] brackets when specifying the Array name.
  • Elements Number of elements to sort.
  • Order [OPTIONAL] ASCENDING or DESCENDING specifies sort order Default is ASCENDING order.

Examples:

 
 QSORT Buffer$, 1000, DESCENDING

 QSORT MyNum!, 300, ASCENDING

 QSORT TestScores#, 50

BCX Console Sample Programs using QSORT function.

S129.bas

QSORTIDX statement

Purpose: QSORTIDX will sort, very quickly, a table of data arrays. Note well that the array on which the sort is done must be a string or char data type and can not be a numerical data type.


 Syntax 1:
 
 QSORTIDX IndexArray, NumRecs, ArrayToSort, KeyField

 Parameters:

  • IndexArray STATIC or DYNAMIC(single dimension) integer array that is to be sorted and is used to reference ArrayToSort. Note that IndexArray is automatically initialized before being sorted.
  • NumRecs Number of records contained in ArrayToSort.
  • ArrayToSort can be
    1. a two dimension DYNAMIC string array with the first dimension being the number of fields, the second dimension being the number of records.

      For example, when ArrayToSort is dimensioned as

      
       DIM DYNAMIC ArrayToSort$[3, 1000]
      
      

      a two dimensional array is created which can store three fields with each field containing space for one thousand items (strings in this instance) each with a default length of 2048 bytes.

      or

    2. If you wish to specify a custom cell length, you can do so by adding a third argument when dimensioning the ArrayToSort which in this case is a three dimension DYNAMIC string array with the first dimension being the number of fields, the second dimension being the number of records, and the third dimension the custom cell length. An appended data type specifier, AS CHAR, is also required.

      Adding a third argument, 32, to the example above, like this,

      
       DIM DYNAMIC ArrayToSort[3, 1000, 32] AS CHAR
      
      

      would create a two dimensional array which can store three fields with each field containing space for one thousand, 32 byte custom length, string items.

  • KeyField Indicates by which field(element) in ArrayToSort to sort.

Example 1:


 CONST  GivenName = 0
 CONST  Surname   = 1
 CONST  Address   = 2
 CONST  City   = 3
 CONST  State  = 0
 CONST  Zip    = 1
 CONST  Income = 0
 CONST  NumRecs = 3
 CONST  LastRec = NumRecs - 1
  
 DIM i%
   
 DIM DYNAMIC IdxAry[NumRecs]
   
 'GivenName, Surname , Address, City
 DIM DYNAMIC ContactAry1$[4, NumRecs]
 '4 fields(2048 chars each), 3 records
   
 'State and Zip code
 DIM DYNAMIC ContactAry2$[2, NumRecs, 10] AS CHAR
 '2 fields(10 chars each), 3 records
   
 'Annual income
 DIM DYNAMIC ContactAry3#[1, NumRecs]
 '1 field(double precision float), 3 records
  
 ContactAry1$[GivenName, 0] = "Katrina" 
 ContactAry1$[Surname, 0]   = "Van Tassel"
 ContactAry1$[Address, 0]   = "18 North Broadway"
 ContactAry1$[City, 0]      = "Sleepy Hollow"
 ContactAry2$[State, 0]     = "N.Y."
 ContactAry2$[Zip, 0]       = "10591-1806"
 ContactAry3#[Income, 0]    = 1500250.98
  
 ContactAry1$[GivenName, 1] = "Ichabod"
 ContactAry1$[Surname, 1]   = "Crane"
 ContactAry1$[Address, 1]   = "Route 9H"
 ContactAry1$[City, 1]      = "Kinderhook"
 ContactAry2$[State, 1]     = "NY"
 ContactAry2$[Zip, 1]       = "12106"
 ContactAry3#[Income, 1]    = 40.96
  
 ContactAry1$[GivenName, 2] = "Abraham 'Brom Bones'"
 ContactAry1$[Surname, 2]   = "Van Brunt"
 ContactAry1$[Address, 2]   = "540 North Broadway"
 ContactAry1$[City, 2]      = "North Tarrytown"
 ContactAry2$[State, 2]     = "New York"
 ContactAry2$[Zip, 2]       = "10591"
 ContactAry3#[Income, 2]    = 50025.98
  
 ' Sort by element in ContactAry1
 CALL SortAndPrint(ContactAry1, Surname, "Surname")
 
 ' Sort by element in ContactAry2
 CALL SortAndPrint(ContactAry2, Zip, "Zip")
 
 END
  
 SUB SortAndPrint(TheArray AS CHAR PTR PTR PTR, key, KeyNameNdx$) 
 
   QSORTIDX IdxAry, NumRecs, TheArray, key 
   FOR i% = 0 TO LastRec
     PRINT "Record ", i%, " sorted by key ", KeyNameNdx$
     PRINT ContactAry1$[GivenName, IdxAry[i]] 'GivenName
     PRINT ContactAry1$[Surname, IdxAry[i]]   'Surname
     PRINT ContactAry1$[Address, IdxAry[i]]   'Addressess
     PRINT ContactAry1$[City, IdxAry[i]]      'City
     PRINT ContactAry2$[State, IdxAry[i]]     'State
     PRINT ContactAry2$[Zip, IdxAry[i]]       'Zip
     PRINT USING$("$#,###,###.##", _
     ContactAry3#[Income, IdxAry[i]])         'Income
     PRINT " "
   NEXT i%
 END SUB
  
 KEYPRESS


 Syntax 2:
 
 QSORTIDX IndexArray, NumRecs, UDT.MemberToSort, 0

 Parameters:

  • IndexArray is a DYNAMIC (single dimension) integer array that is to be sorted and used to reference the UDT.MemberToSort array. Note that IndexArray is automatically initialized before being sorted.
  • NumRecs Number of records contained in the UDT.MemberToSort array.
  • UDT.MemberToSort specifies the user defined type array member by which the sort is to be made.
  • KeyField is set to 0.

Example 2: This example uses user defined type records.


 TYPE MyRecord
   GivenName[50] AS CHAR
   Surname[50] AS CHAR
   Age AS INTEGER
   Address[100] AS CHAR
   City[100] AS CHAR
   State[30] AS CHAR
   Zip[15] AS CHAR
   Income AS INTEGER
 END TYPE
 
 DIM NumRecs% = 3
 DIM LastRec%
 LastRec% = NumRecs - 1
 
 GLOBAL DYNAMIC Addr[NumRecs] AS MyRecord
 GLOBAL DYNAMIC Idx[NumRecs]
 
 Addr[0].GivenName$ = "Katrina"
 Addr[0].Surname$   = "Van Tassel"
 Addr[0].Age%       = 16
 Addr[0].Address$   = "18 North Broadway"
 Addr[0].City$      = "Sleepy Hollow"
 Addr[0].State$     = "N.Y."
 Addr[0].Zip$       = "10591-1806"
 Addr[0].Income%    = 150
 
 Addr[1].GivenName$ = "Ichabod"   
 Addr[1].Surname$   = "Crane"     
 Addr[1].Age%       = 48          
 Addr[1].Address$   = "Route 9H"  
 Addr[1].City$      = "Kinderhook"
 Addr[1].State$     = "NY"        
 Addr[1].Zip$       = "12106"     
 Addr[1].Income%    = 40     
                    
 Addr[2].GivenName$ = "Abraham 'Brom Bones'"
 Addr[2].Surname$   = "Van Brunt"           
 Addr[2].Age%       = 19                    
 Addr[2].Address$   = "540 North Broadway"  
 Addr[2].City$      = "North Tarrytown"     
 Addr[2].State$     = "New York"            
 Addr[2].Zip$       = "10591"               
 Addr[2].Income%    = 5
 
 CALL SortAndPrint(0)
 CALL SortAndPrint(1) 
 CALL SortAndPrint(2) 
 CALL SortAndPrint(3) 
 CALL SortAndPrint(4) 
 CALL SortAndPrint(5) 
 CALL SortAndPrint(6) 
 CALL SortAndPrint(7) 
 
 SUB SortAndPrint(field)
 
   SELECT CASE field
   CASE 0
     QSORTIDX Idx, 3, Addr.GivenName, 0
   CASE 1
     QSORTIDX Idx, 3, Addr.Surname, 0
   CASE 2
     QSORTIDX Idx, 3, Addr.Age, 0
   CASE 3
     QSORTIDX Idx, 3, Addr.Address, 0
   CASE 4
     QSORTIDX Idx, 3, Addr.City, 0
   CASE 5
     QSORTIDX Idx, 3, Addr.State, 0
   CASE 6
     QSORTIDX Idx, 3, Addr.Zip, 0
   CASE 7
     QSORTIDX Idx, 3, Addr.Income, 0
   END SELECT
 
   FOR INTEGER i% = 0 TO LastRec
     PRINT "Record ", i%, " sorted by key ", field%
     PRINT Addr[Idx[i]].GivenName$ 'GivenName
     PRINT Addr[Idx[i]].Surname$   'Surname
     PRINT Addr[Idx[i]].Age%       'Surname
     PRINT Addr[Idx[i]].Address$   'Addressess
     PRINT Addr[Idx[i]].City$      'City
     PRINT Addr[Idx[i]].State$     'State
     PRINT Addr[Idx[i]].Zip$       'Zip
     PRINT Addr[Idx[i]].Income%    'Income
     PRINT " "
   NEXT i%
 
 END SUB            


 Syntax 3:
 
 QSORTIDX 0, NumRecs, UDT.MemberToSort, 0

 Parameters:

  • IndexArray is set to 0.
  • NumRecs Number of records contained in the UDT.MemberToSort array.
  • UDT.MemberToSort specifies the user defined type array member by which the sort is to be made.
  • KeyField is set to 0.

Example 3: This example uses user defined type records without an index array.


 TYPE MyRecord
   GivenName[50] AS CHAR
   Surname[50] AS CHAR
   Age AS INTEGER
   Address[100] AS CHAR
   City[100] AS CHAR
   State[30] AS CHAR
   Zip[15] AS CHAR
   Income AS INTEGER
 END TYPE
 
 DIM NumRecs% = 3
 DIM LastRec%
 LastRec% = NumRecs - 1
 
 GLOBAL DYNAMIC Addr[NumRecs] AS MyRecord
 
 Addr[0].GivenName$ = "Katrina"
 Addr[0].Surname$   = "Van Tassel"
 Addr[0].Age%       = 16
 Addr[0].Address$   = "18 North Broadway"
 Addr[0].City$      = "Sleepy Hollow"
 Addr[0].State$     = "N.Y."
 Addr[0].Zip$       = "10591-1806"
 Addr[0].Income%    = 150
 
 Addr[1].GivenName$ = "Ichabod"   
 Addr[1].Surname$   = "Crane"     
 Addr[1].Age%       = 48          
 Addr[1].Address$   = "Route 9H"  
 Addr[1].City$      = "Kinderhook"
 Addr[1].State$     = "NY"        
 Addr[1].Zip$       = "12106"     
 Addr[1].Income%    = 40     
                    
 Addr[2].GivenName$ = "Abraham 'Brom Bones'"
 Addr[2].Surname$   = "Van Brunt"           
 Addr[2].Age%       = 19                    
 Addr[2].Address$   = "540 North Broadway"  
 Addr[2].City$      = "North Tarrytown"     
 Addr[2].State$     = "New York"            
 Addr[2].Zip$       = "10591"               
 Addr[2].Income%    = 5
 
 CALL SortAndPrint(0)
 CALL SortAndPrint(1) 
 CALL SortAndPrint(2) 
 CALL SortAndPrint(3) 
 CALL SortAndPrint(4) 
 CALL SortAndPrint(5) 
 CALL SortAndPrint(6) 
 CALL SortAndPrint(7) 
 
 SUB SortAndPrint(field)
 
   SELECT CASE field
   CASE 0
     QSORTIDX 0, 3, Addr.GivenName, 0
   CASE 1
     QSORTIDX 0, 3, Addr.Surname, 0
   CASE 2
     QSORTIDX 0, 3, Addr.Age, 0
   CASE 3
     QSORTIDX 0, 3, Addr.Address, 0
   CASE 4
     QSORTIDX 0, 3, Addr.City, 0
   CASE 5
     QSORTIDX 0, 3, Addr.State, 0
   CASE 6
     QSORTIDX 0, 3, Addr.Zip, 0
   CASE 7
     QSORTIDX 0, 3, Addr.Income, 0
   END SELECT
 
   FOR INTEGER i% = 0 TO LastRec
     PRINT "Record ", i%, " sorted by key ", field%
     PRINT Addr[i].GivenName$ 'GivenName
     PRINT Addr[i].Surname$   'Surname
     PRINT Addr[i].Age%       'Surname
     PRINT Addr[i].Address$   'Addressess
     PRINT Addr[i].City$      'City
     PRINT Addr[i].State$     'State
     PRINT Addr[i].Zip$       'Zip
     PRINT Addr[i].Income%    'Income
     PRINT " "
   NEXT i%
 
 END SUB            

Example 4: This example QSORTIDX syntax sorts the records in the range 5 thru 15.

 
 QSORTIDX 0, 10, &UDT[5].MemberToSort, 0