Announcement

Collapse
No announcement yet.

Help with ARRAY SORT

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

  • Help with ARRAY SORT

    Could someone please help me with sorting an array using ARRAY SORT? Suppose that I have:
    Dim CemNam$(20)

    and only the first five items are occupied; the rest are blank. They are:
    CemNam$(1)="Wilson"
    CemNam$(2)="Smith"
    CemNam$(3)="Thomas"
    CemNam$(4)="Brown"
    CemNam$(5)="Anderson"


    When I do an ARRAY SORT CemNam$() the names are sorted, but they are moved to the end of the array. I understand why this occurs, but is there a way to keep them at the front quickly? My routine takes about eight seconds.

    I was attempting to do something like this:
    ARRAY SORT CemNam$() For 1 to Kount

    If Kount=5, I mistakengly thought it would sort just the first five elements. Could someone show me what I am misunderstanding please?

    Thank you.

    Robert

  • #2
    From the documentation...

    Together, index and count specify the portion of darray to be sorted. index specifies the element at which the sort is to begin, while count specifies the number of consecutive elements to be sorted. If index is omitted, the sort begins at the first element of darray. If count is omitted, the array is sorted from element index to the last element of darray. If both are omitted, the entire array is sorted:

    DIM A&(1 TO 99)
    ARRAY SORT A&(5) 'sorts elements 5..99 of A&
    ARRAY SORT A&() FOR 10 'sorts elements 1..10 of A&
    ARRAY SORT A&(9) FOR 20 'sorts elements 9..28 of A&
    ARRAY SORT A&() 'sorts elements 1..99 of A&
    So depending on how your array is DIM'd you would do something like this:
    ARRAY SORT CemNam$(1) for lngRecordCount
    or
    ARRAY SORT CemNam$() for lngRecordCount
    This NORMALLY would be the correct one in your case for DIM CemNam$(20).
    BUT...When you DIM CemNam$(20) the array actually starts at CemNam$(0) and goes through CemNam$(20).
    Being you don't seem to be using CemNam$(0) in your code
    it will sort the first item into CemNam$(0), 2nd into CemNam$(1), etc... which is not what I think you are looking for.
    If you if you want to use ARRAY SORT CemNam$() for lngRecordCount you would need to do one of two things:
    1. Modify all your code to account for a "0" based index.
    2. Change to DIM CemNam$(1 to 20)

    Last edited by George Bleck; 13 Jul 2009, 02:44 PM.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

    Comment


    • #3
      >When I do an ARRAY SORT CemNam$() .....

      You are forgetting all the null elements: element zero plus elements six thru twenty.

      (Element zero is the one lots of folks forget about).

      See both "Start" and "count" options in help file.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Originally posted by George Bleck View Post
        From the documentation...
        If you if you want to use [/COLOR]ARRAY SORT CemNam$() for lngRecordCount you would need to do one of two things:
        1. Modify all your code to account for a "0" based index.
        2. Change to DIM CemNam$(1 to 20)

        [/INDENT]
        That fixed it!! Thanks. -Robert

        Comment

        Working...
        X