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.