I have something like this:
$i = 0;
foreach($tracks['results'] as $track){
$trackName[$i] = $track['name'];
$trackPlaycount[$track['name']] = $track['playcount'];
$trackPercent[$track['name']] = $track['percent'];
$i++;
}
$this->trackName = $trackName;
$this->trackPlaycount = $trackPlaycount;
$this->trackPercent = $trackPercent;
how could I sort these objects by playcount? From what I have read so far I understand I should probably create a compare function and then make it work with usort(), right? But I'm not quite sure how to accomplish that...
thank you
edit: ok, so now I've got it like this:
public function firstmethod(){
// there are some more parameters here of course
// but they worked before, not important for the problem
$i = 0;
foreach($tracks['results'] as $track){
$trackName[$i] = $track['name'];
$trackPlaycount[$track['name']] = $track['playcount'];
$trackPercent[$track['name']] = $track['percent'];
// this part is new
$tracksArray[$i] = array(
'name' => $trackName[$i],
'playcount' => $trackPlaycount[$track['name']],
'percentage' => $trackPercent[$track['name']]
);
$i++;
}
usort($tracksArray, array($this, 'sortByCount'));
$i = 0;
// this is to put the new sorted array into the old variables?
foreach($tracksArray as $temp){
$trackName[$i] = $temp['name'];
$trackPlaycount[$trackName[$i]] = $temp['playcount'];
$trackPercent[$trackName[$i]] = $temp['percentage'];
$i++;
}
$this->trackName = $trackName;
$this->trackPlaycount = $trackPlaycount;
$this->trackPercent = $trackPercent;
}
public function sortByCount($a, $b){
if($a["playcount"] == $b["playcount"]) {
return 0;
}
return ($a["playcount"] < $b["playcount"]) ? -1 : 1;
}
this now works... thank you everyone