# Newbie Question: Why Isn't arsort Sorting?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Here's the array I'm trying to sort:

\$Differences = Array[3]
Brass = (double) 601.88
Iron = (double) 376.18
Steel = (double) 526.65

Here's the code:

arsort(\$Differences, SORT_NUMERIC);

It seems as though arsort isn't sorting - the array is in the exact same
order, after arsort runs, as it was before. What am I missing?

Thanks very much in advance to all for any info.

## Re: Newbie Question: Why Isn't arsort Sorting?

In our last episode,
the lovely and talented Vik Rubenfeld

Pretty late in September to be doing your first homework assignment, isn't
it?  Anyway that 'Array[3]' is nonsense and causes me to suspect that you
did not enter the array properly.

What do you get when you print_r(\$Differences)?

if you don't get

Array
(
[Brass] => 601.88
[Iron] => 376.18
[Steel] => 526.65
)

you have not entered the array you thought you did.

Yes, that would work, if your array is properly entered, but as we said in
the old country "Garbage in, garbage out."

You do know arsort gives you descending order.  If you want ascending order,
use asort.

#!/usr/local/bin/php
<?php

\$Differences = array(
Brass => 601.88,
Iron => 376.18,
Steel => 526.65);

print_r(\$Differences);

/* Array
(
[Brass] => 601.88
[Iron] => 376.18
[Steel] => 526.65
)
is what you get here.
*/

arsort(\$Differences,SORT_NUMERIC);

print_r(\$Differences);

/* Array
(
[Brass] => 601.88
[Steel] => 526.65
[Iron] => 376.18
)
is what you get here.  Of course there is no reading your mind, and if
you meant a different array or should have used asort, I cannot know that
at this end.

*/

?>

--
Lars Eighner     <http://larseighner.com/ <http://myspace.com/larseighner
Countdown: 481 days to go.
What do you do when you're debranded?

## Re: Newbie Question: Why Isn't arsort Sorting?

Lars, you owe me an apology. The confusion is, that I was showing the
contents of the array - not the PHP code that created it, as you
mistakenly assumed.   I listed the contents of the array, in the way
they are displayed in the variables window in Zend Studio.

However, running your code, did provide the answer. The order of the
variables as they are displayed in the Zend Studio variables window,
does not change after arsort, even though the print order of the array
has indeed changed.

## Re: Newbie Question: Why Isn't arsort Sorting?

In our last episode,
the lovely and talented Vik Rubenfeld