-1
array(3) { [0]=> int(1) [1]=> int(100) [2]=> int(6) }

I want to sort the following array in numerical order max - min.

Expected output:

array(3) { [0]=> int(100) [1]=> int(6) [2]=> int(1) }

However, when I use asort($arr, SORT_NUMERIC) or sort($arr, SORT_NUMERIC) I get this unexpected result:

array(3) { [0]=> int(6) [1]=> int(100) [2]=> int(1) }
1

1 Answer 1

-1

PHP sort() has SORT_NATURAL flag.

It should be...

sort($array, SORT_NATURAL);
// or
rsort($array, SORT_NATURAL);

Tests:

$array = [1, 100, 6];

sort($array, SORT_NATURAL);

var_dump($array);

rsort($array, SORT_NATURAL);
var_dump($array);

array (size=3) 0 => int 1 1 => int 6 2 => int 100

array (size=3) 0 => int 100 1 => int 6 2 => int 1

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.