Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an array

SitesMasstec

Programmer
Sep 26, 2010
558
Brasil
Hello colleagues!

I have been trying to sort an array in alphabetical order, but errors appear:

Code:
PUBLIC ARRAY Cabine(30)
FOR I=1 TO 30
    IF XTIPOCAB(I)<>SPACE(3)
       Cabine(I)=XTIPOCAB(I) + " - " + XCATEGCAB(I) + " ("+ ALLTRIM(XNOMECAB(I)) + ") "
    ENDIF
NEXT I
ASORT(Cabine)     && Sort the array

What is wrong?

Thank you.
 
The program stops executing in line:
ASORT(Cabine) && Sort the array
and presents the error "Data type mismatched"
 
Just to simplify.
I created a new Form, and put a Combobox in it. In the Combobox GotFocus event I put the code:
Code:
PUBLIC ARRAY Texto(10)

Texto(1)="Paul"
Texto(2)="Charles"
Texto(3)="Mary"

ASORT(Texto)    && Sort the array


With This
    FOR I=1 TO 3
       .AddItem(Texto(I))
    NEXT I
    .ListIndex=1
ENDWITH

When executing the error "Data type mismatched" appears.
 
Just to simplify.
I created a new Form, and put a Combobox in it. In the Combobox GotFocus event I put the code:
Code:
PUBLIC ARRAY Texto(10)

Texto(1)="Paul"
Texto(2)="Charles"
Texto(3)="Mary"

ASORT(Texto)    && Sort the array


With This
    FOR I=1 TO 3
       .AddItem(Texto(I))
    NEXT I
    .ListIndex=1
ENDWITH

When executing the error "Data type mismatched" appears.
=asort(texto)
 
This it happens because your array does not have the right dimension: 3.
So you have 2 solutions:
1. change your first line to: PUBLIC ARRAY Texto(3)
2. add more 'empty' items to your array till you reach your dimension (10).
 
Kudos to costin77. He saw the problem while I was still trying to think about it. :)

To add to his post, when you initialize an array, all the values are logical .f. so if you try to sort it when some values are character and some are logical it will obviously give an error.
 
Code:
PUBLIC ARRAY Cabine(30)
Store "" TO Cabine && initialize all elements with empty string (otherwise they're .f.)

As Doug said, array elements are intied with .F.
Mixed with strings in some elements sorting fails on different types.

SitesMasstec, imagine how sorting works, it compares elements and puts the lower one first, the larger one second, when it comes to comparing .f.<"string" or .f.>"string" causes a type error - and you have both .f. values and strings in your array. All elements of the array have to be the same type for sorting, otherwise you're "comparing appples with oranges".

The simplest way to store something to every element with a value like an empty string is to use what I show above. So don't just declare the array, fill it. Your for loop only fills some elements, because it all depends on the content of XTIPOCAB, see your IF. That's what fails.

Other ways to cope with this: Only dimension the array as large as you actually have data, remove empty elements, redimension the array. Either shorten it at the end or only add elements when needed, so start with one element and then add further as necessary.
 
Last edited:
As a rule of thumb, when you get an error like that, it's giving you the exact reason for the error, a Data Mismatch Error, so you just need to take a look a data.

You only can sort an array if all the values are the same type. In fact, you can even sort an array of logical variables, where .f. is on top and .t. is on the bottom, but you can't have a mixture of variable types.

So, when you come across an error like this, put something there so you can see all the values just before the line that triggers the error. For example, in this particular case, if you put a suspend just before the sort, you can look at the values in your array from the Debugger.

We can't see the reason for the first example because you are populating the array from a User Designed Function, but as others pointed out in your second example, you created an array with 10 values, but only populated 3. So, for the first sample, your User Defined Function must be returning more than one data type as a result.

If that's happening in your function, it's likely due to a conditional logic problem. All function return a logical .T. value unless you implicitly have a RETURN clause with a different value. So, if you have a function that has multiple IFs CASES, etc. that eventually return something like a string, and you don't return a specific value for every logical condition, your function will occasionally return .T. and unlike other languages, FoxPro allows an array to have values of different types at the same time... the only catch as I mentioned is you can't sort an array with multiple types.
 

Part and Inventory Search

Sponsor

Back
Top