Possible Duplicate:
Sorting xml with simpleXML/xpath?
I'm using SimpleXML and I'm trying to sort my list by the amount of views it has had. I have a generic foreach function that runs after the sorting as below:
if($filter=="popular"){
$sort=$x->xpath('//example[views]');
function cmp($a, $b){
if ( $a->id > $b->id) return -1;
if ( $a->id < $b->id) return 1;
return 0;
}
usort($sort, cmp); //this isn't working?
} else {
$sort = $x->example;
}
foreach($sort as $q){ ...
My XML looks something like:
<list>
<example>
<id>1</id>
<name>bob<name>
<views>5</views>
</example>
<example>
<id>2</id>
<name>fred<name>
<views>18</views>
</example>
<example>
<id>3</id>
<name>alfie<name>
<views>0</views>
</example>
</list>
So I just need a way to sort by the amount of views each example has had in DESC order - highest views to lowest views.
Any ideas?
Many thanks :)