cooldude832 Posted January 5, 2008 Share Posted January 5, 2008 I have an array populated by <?php while($row = fetch){ $test = $row['Hits']*$row['Deals']*$row['Rating']; $data[$i]['Name'] = $row['Username']; $data[$i]['id'] = $row['UserID']; $data[$i]['Score'] = $test; } ?> it looks like Array ( [0] => Array ( [Name] => cooldude_832 [id] => 1 [score] => 188.628 ) [1] => Array ( [Name] => TheDealerUK [id] => 2 [score] => -669.3 ) ) I want to sort the array by the $data[]['Score'] field any ideas? I tried making the scores the keys and then using a ksort, but I want to destroy the keys and have it renumber them so any ideas? Quote Link to comment Share on other sites More sharing options...
neylitalo Posted January 5, 2008 Share Posted January 5, 2008 When you take a computer science class, one of the very first things they (should) teach you is how to implement a quicksort. For this, you'll need to write your own sort implementation - a quicksort is a good start. Note that quicksort isn't very efficient, relative to other methods of sorting, but it's a good place to get started. If you want to, you can research other sorting algorithms. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 usort() <?php $data = array ( array ( 'Name' => 'cooldude_832', 'id' => 1, 'Score' => 188.628 ), array ( 'Name' => 'TheDealerUK', 'id' => 2, 'Score' => -669.3 ) ); function scoresort($a, $b) { return $a['score'] - $b['score']; } usort ($data , 'scoresort'); echo '<pre>', print_r($data, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 5, 2008 Author Share Posted January 5, 2008 I tried your function (which didn't make sense to me) doing <?php function scoresort($a, $b){ return $a['score'] - $b['score']; } print_r($data); usort ($data , 'scoresort'); print_r($data); ?> then it returns Array ( [0] => Array ( [Name] => cooldude_832 [id] => 1 [score] => 62.37 ) [1] => Array ( [Name] => TheDealerUK [id] => 2 [score] => 41.44 ) ) Array ( [0] => Array ( [Name] => TheDealerUK [id] => 2 [score] => 41.44 ) [1] => Array ( [Name] => cooldude_832 [id] => 1 [score] => 62.37 ) ) as you can see it is going backwards of what I wanted I'll just rsort it from there and be set? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 41.44 followed by 62.37 looks sorted to me Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted January 5, 2008 Author Share Posted January 5, 2008 shoulda said i want high to low its a quality score developed off some fields in mysql so I want to rank the top 100 users so 154 123.54 100.543 52.65 13.4 -0.234 -123 etc. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 5, 2008 Share Posted January 5, 2008 Sorry about that, no damn clairvoyance. reverse the sign of the returned result for a descending sort function scoresort($a, $b){ return $b['score'] - $a['score']; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.