if I have an array like this (wihin a loop – so it is filled with more than 1 item of course):
$returnArray[] = array("type" => $dirinfo[0],"fileSize" => $this->ByteSize($dirinfo[1]),"fileName" => $dirinfo[2]);
The field “type” can be “folder” or “file”, but they are mixed together,
so like folder,file,file,folder,folder,file,etc
I would like to sort with the folders on top first and then the files… (like windows folder display behavior)
I’ve played with array_multisort, but just can’t get it to work…what should I do?
their example is this 9though I would like the same array returned just sorted, not a new array.:
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
so I made this:
// tmp try sorting
foreach ($returnArray as $key => $row) {
$type[$key] = $row['type'];
$fileSize[$key] = $row['fileSize'];
$fileName[$key] = $row['fileName']
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($type, SORT_DESC, $fileName, SORT_ASC, $fileSize, SORT_ASC, $rfileArray);
The first stop for such work is
usort:Basic usage is pretty simple:
From PHP 5.3 onwards, you can write the comparison function inline:
See also the very nice comparison of array sorting functions.