No announcement yet.

Sorting an Array?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Sorting an Array?

    Ok, this may be dumb, but here goes.
    I am trying to sort an array alphabeticaly 1 to 30 and by date
    in the format of mm/dd.
    I have used the (index) and the (for) and even the (from) (to)
    I can get it to sort alpha with no problem but, when I try to
    do it within date it goes out of order.

    Can anyone help me???
    I would appreciate any help.

  • #2
    I am assuming you are sorting by the string and then the date. For example: Smith 02/06

    From your description, it looks like you have something like:
    Smith 02/06
    Computer 05/31

    What you need to do is "flesh-out" the name field, with spaces, to the longest name in the field + 1 then concanotate the date.
    Remember, when evaluating strings, numbers are ALWAYS less than letters because your program sees the numbers as strings. Fleshing out the name field will put the date(s) in their proper alignment for sorting.

    Another approach is to put the string field in one array and the date in another array. Sort on the string field with the 2nd array as a "TAG" array. All this should be explained in the manuals.

    Hope this helps.

    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.


    • #3
      If I can maybe offer some further detail...

      When sorting strings, ARRAY SORT sorts the string data based on the ASCII code of each character. Therefore, space characters become important is you are combining "fields" of data into a string to be sorted. The only way to achieve this is to format the string data into "columns". Because the ascii value of each character is important, string representations of numeric values such as dates, need to be handled carefully, and reformatted if necessary.

      In other words, you need to format your string data so that each sub-string you build into the string array starts and ends at the same character position in every single array subscripts.

      For example, lets build an array containing a name and a date.

      We know that the "name" field will have a maximum of 30 characters - if a name is less than this, we need to add additional spaces to the end of the name to bring it up to 30 characters in length.

      If the array is called A$(), and the name is 30 chars wide and it is followed by the date (in the format YYYYMMDD which can then be sorted alphanumerically):
      FOR X& = 1 TO NumCustomers&
         ' here you could get your actual data, is from a disk file...
         GET #DataBase&, CustRecNumber&, CustomerUDT
         ' the field CustomerUDT.CustName is an 30 character ASCIIZ 
         ' string (ASCIIZ * 31, to allow for the NUL byte),
         ' so we need to handle the padding ourselves.  The date 
         ' field is always 8 characters wide (STRING * 8) so the
         ' string is already padded (and formatted as YYMMDD)
         ' A$()= "<------------NAME------------> <-DATE->"
         A$(X&) = LEFT$(CustomerUDT.CustName + SPACE(30), 30) + " " + CustomerUDT.BillDate 
      NEXT X&
      ' Now sort the array using the date section as the sort key
      ARRAY SORT A$(1) FOR NumCustomers&, FROM 32 to 39
      Because the "name" section of the array is a fixed width, sorting the array on the whole section of each string will also have the effect of "sub-sorting" entries that have the same name, but with different dates. ie, SMITH...19991201, SMITH...19991202, etc

      The key to sorting dates this way is that we have stored the date using the YYYYMMDD or MMDD format. If we stick to this storage format, the sorted list will occur in date order automatically.

      For example, the 13th Jan (0113) should appear before 1st Dec (1201). If we used DDMM or DDMMYY, then the same dates (1301 and 0112) would appear to be sorted in the reverse order!

      I hope this helps!

      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      mailto:[email protected]