I have two arrays in php
$arrNum = array(1.7, 1.52, 0.01, 0.11);
$arrStr = array('1.7', '1.52', '0.01', '0.11');
Note that the second array is the same as the first one, except it has the values as strings.
Is it possible for a sorting or max/min operation to return a different result for the second array just because they are strings?
Can I always do an operation that requires value comparison on the string array and get the exact same result that I would have gotten if I had done it on the numeric version of the array?
The documentation is clear, imo:
E.g. for
sort():And it takes a parameter to define how the values are treated:
But it also has a warning:
Now for
max():and
Update:
To answer your question: If you want to make absolutly sure that the sorting is correct, you know that the strings always contains numbers and you don’t trust the string comparison, then specify to sort them numerically: