The Xbase++ Programmer
ARRAY SORTING
Home
Articles
Source Code
Tools
Xbase++ FAQs
  Arrays
  • Array Sorting

  •   FAQs
  • FAQ Pages
  • How to Submit
  • More FAQs to come

  •   OOP
  • Possible Memory Leak

  • Links
    Title: Array Sorting
    Version: 1.2
    Author: Michael McVicker
    Date: 03/26/2009
        When it comes to Sorting Arrays, Remember...
    It is important to remember that the function ASORT() will only sort a Single
    Dimensional Array.

    When a Multi-Dimensional Array needs to be sorted, you MUST pass a Codeblock to
    handle the comparison of array values.

    The reason is that ASORT only handles a single dimension array so it passes each
    array element to the comparator to be evaluated.

    Example:
    If we have an array such as;
      aMyArray := { { "ABC", 789 }, ;
    { "DEF", 456 }, ;
    { "GHI", 123 } }

    Using a simple ASORT, by default what is passed is { "ABC", 789 } as the first
    comparison, { "DEF", 456 }, as the second and { "GHI", 123 } as the third.
    This is probably not what you would want for you sort.

    This is why we need a codeblock.
    In order to sort on the character string, we need to look at only the first element
    of each subarray. To accomplish this, ASORT passes each element to a provided
    codeblock and leaves it up to the programmer to figure out which element in the
    subaray to evaluate.

    From the Xbase++ Help example;
      ASort( aMyArray,,, {|aX,aY| aX[1] > aY[1] } )

    ASORT will pass two values to figure out which is to placed higher or lower in the
    resulent order.

    In our example, we first compare { "ABC", 789 } and { "DEF", 456 }.
    These subarays are passed to our codeblock and identified as aX and aY respectively.
    So now aX = { "ABC", 789 } and aY = { "DEF", 456 }.
    They are evaluated in our codeblock as aX[1] and aY[1] ( "ABC" and "DEF" ).

    In order to sort on the numeric element of our subarrays, simply change the aX[1] to aX[2] and aY[1] to aY[2].


     

     

     

     
     
    Helping you get more out of the Xbase++ language.
    For more information or to contribute, please contact:Support@XbaseProgrammer.com