View Full Version : array sorting function
heaps21
08-17-2004, 09:52 PM
Hi, I need a function that does the following:
As I loop through the results of my database query(which returns id and a numerical value) if the value is < the first element of an array then that value becomes teh first element, pushing all other values up. If not, keep searching through the array until the previous element is < and the next element is >, and slot it in tehre. Is there a function that will do this, if not can anyone give me any pointers as to how to approach this.
Im guessing it will be something to do wiwth the multisort function but i cant figure out how to store each id and value in a different array when i dont know how many results my query will return.
Thanks in advance.
mordred
08-17-2004, 10:29 PM
I'm not totally sure I understood every iota of your post, but in your scenario, I would first append every value from a DB result row to your array. After you finished iterating, all values are at the end of the array, obviously unsorted. Then you run sort() or usort() on the array to sort it, once you have all values you need. I think that's a simpler approach than trying to put it at the right slot on each iteration.
heaps21
08-17-2004, 10:37 PM
I'm not totally sure I understood every iota of your post,
:D
Then you run sort() or usort() on the array to sort it, once you have all values you need. I think that's a simpler approach than trying to put it at the right slot on each iteration.
The problem with that is I have 2 fields from every db record - id and value and i need to sort it by value but obviously still know the id. I've only ever needed to use basic array functions so i dont know how to deal with it.
mordred
08-17-2004, 11:10 PM
I'm not sure I follow. How did you intend to store the id -> value association in your initial example? I thought you only wanted to put the retrieved values at the right places of this array.
Can you post a short example how your array in which you want to store values should look like after it has been sorted?
heaps21
08-17-2004, 11:24 PM
hmm, guess my initial explanation wasnt that great... ive been looking at php.net under asort and the example it gives with fruit is pretty much what i want to be able to do, only building up the array as per the database records.
So, retrieved from the database are $id and $value ordered by id. Some manipulation of value takes place, and then i need to store each $id and $value in an array of somesort which then needs to be sorted by value. Once sorted though i still need to know which $id the $value relates to.
Sorry for my poor explanations, believe me it would have been even worse had i tried to explain it anymore technically! :)
mordred
08-18-2004, 08:55 AM
I've hacked a short example together how I understand your problem. It illustrates the use of usort() to have custom function sort your array.
$test = array(
// old values
array('id' => 2345, 'value' => 10),
array('id' => 1456, 'value' => 20),
array('id' => 8978, 'value' => 30),
array('id' => 1354, 'value' => 40),
array('id' => 9875, 'value' => 50),
// new ones from DB result rows, appended to this array
array('id' => 3546, 'value' => 21),
array('id' => 7865, 'value' => 35),
array('id' => 1000, 'value' => 5),
);
// compare function to decide how items get sorted
function sortById($a, $b) {
if ($a['value'] == $b['value']) {
return 0;
}
return ($a['value'] < $b['value']) ? -1 : 1;
}
// sort the array with a custom sort function
usort($test, 'sortById');
// show result
var_dump($test);
Should you have any questions regarding this code, feel free to ask.
hth
sad69
08-18-2004, 09:17 PM
I wonder if you'd want to implement a binary search tree. I wouldn't be surprised if one has already been implemented in PHP. Then an inorder traversal will return the sorted array.
I'm not sure which route is more efficient.. the usort() function seems simpler to deal with.. it may depend on the number of elements you plan on working with as well.
Sadiq.
heaps21
08-28-2004, 02:53 PM
Mordred, that code is just what I needed - thanks!
My next question is this: Once the usort has been performed i need to put the id's in a normal array (i.e. $array[0]=$id1, $array[1]=$id2 etc.) in their new order but I dont know how to access them to do this.
thanks.
mordred
08-28-2004, 08:16 PM
If I understood you correctly, then it's not difficult to implement at all. You have two possible approaches. The standard one would be to iterate through $test and append the id values to a new array:
$array = array();
for ($i = 0; $i < count($test); $i++) {
$array[] = $test[$i]['id'];
}
As an alternative and slightly influenced from functional programming, you can use array_map() to do the hard work of transforming the input array.
function map($node) {
return $node['id'];
}
$array = array_map('map', $test);
Which one you use is up to you and your personal preferences. I tend to use the latter one once transformation criterias become a little more complex.
ReadMe.txt
08-28-2004, 08:30 PM
what is it you are actually trying to achive here? would it not be a while lot simpler just to insert the info into the DB then return a sorted result set?
AaronW
08-28-2004, 09:57 PM
Like a: SELECT * FROM table ORDER by value, id ASC or something like that?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.