Welcome Guest, Not a member yet? Register   Sign In
sorting arrays
#1

[eluser]new_igniter[/eluser]
Hello

I have a set of arrays I build like this:
Code:
$authorStreamArray[] = array($acount,$username,$userPic,$numFollowers);

I then want to sort this according to $acount

Can someone help me to understand the best way to do this? I have tried diff stuff, but I would love to know how y'all do it.
#2

[eluser]Dam1an[/eluser]
Have you looked at array multisort?
#3

[eluser]new_igniter[/eluser]
yes, Im kind of a noob, so I dont really understand what is really going on there from the examples.
#4

[eluser]Dam1an[/eluser]
I'm impressed... a self classed noob that read the PHP manual? Next you'll be telling me you've read the user guide Tongue

The easiest thing to do, is to try a few examples yourself till you get the result you want.
Its much better to learn by doing 9and getting it wrong first) then by just reading (although you should read as well) examples and copy & pasting it
#5

[eluser]new_igniter[/eluser]
Cool.

on this

Code:
// Obtain a list of columns
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);

if I have 4 keys, do I need to list them all?
#6

[eluser]new_igniter[/eluser]
when I do it the way shown below, I get:

Warning: array_multisort() [function.array-multisort]: Argument #3 is expected to be an array or a sort flag


Code:
foreach ($authorStreamArray as $key => $row) {
        $count[$key] = $row[0];
        $name[$key] = $row[1];
        $pic[$key] = $row[2];
        $follower[$key] = $row[3];
}

array_multisort($count, SORT_DESC, $name, SORT_ASC, $pic, SORT_ASC, $follower, SORT_ASC, $authorStreamArray);
#7

[eluser]new_igniter[/eluser]
ok, this seems to work well:

Code:
foreach ($authorStreamArray as $key => $row) {
        $count[$key] = $row[0];
    }

    array_multisort($count, SORT_DESC, $authorStreamArray);




Theme © iAndrew 2016 - Forum software by © MyBB