cstegner Posted February 25, 2007 Share Posted February 25, 2007 I have a procedure that makes an array of a users contacts from one table. For each contact that it finds it sets an object array with all of the information from the members class. Like so... public function set_contacts($member_id) { $sql = ' SELECT * FROM network WHERE member_id = ' . (int)$member_id .' AND contact="Yes"'; if ($rows = $this->Db->get_rows($sql)) { $Member = new Member(); foreach($rows as $row) { $this->contact_list[] = new Member($row['network_member_id']); } } } Pretty simple, but Now I am trying to figure out how to put these contacts into alphabetical order by the $Member->name. Any ideas on how I could accomplish this? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 usort() with custom sort function <?php function sort_member_by_name ($a, $b) { return strcmp ($a->name, $b->name); } usort ($this->contact_list, 'sort_member_by_name'); ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2007 Share Posted February 25, 2007 Barand, I never could figure out how to use usort or similar functions, so thanks for that! You helped me too Quote Link to comment Share on other sites More sharing options...
Barand Posted February 25, 2007 Share Posted February 25, 2007 Basically it compares each pair of array elements, a and b, in turn by passing them to your function. Your function should return 0 if the elements are considered equal. If a should sort above b, return -1 and if below then return +1. If you are comparing strings as this is, strcmp() does just that. If you want a descending sort, return -strcmp() Quote Link to comment Share on other sites More sharing options...
cstegner Posted February 26, 2007 Author Share Posted February 26, 2007 Barand, Thank you so much, you were a life saver. I read your reasoning behind how this works however am still a bit confused. Don't get me wrong I plugged it in and it works however if you could break down the how a bit more for me I would greatly appreciate it. Just when I feel like I am really starting to get good at this language I feel like a newb all over again. I don't understand how you are calling the function with the quotes around it, or where it is getting $a or $b from. Thanks, again. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 26, 2007 Share Posted February 26, 2007 That part is handled by PHP. You tell it the name of the function to use, the function which has to accept two arguments to compare. That way you can compare them however you want. You don't have to use srtcmp, you just have to make it return the same way stcmp does. Quote Link to comment Share on other sites More sharing options...
sasa Posted February 26, 2007 Share Posted February 26, 2007 try public function set_contacts($member_id) { $sql = ' SELECT * FROM network WHERE member_id = ' . (int)$member_id .' AND contact="Yes"'; $sql = $sql.' OEDER BY name'; if ($rows = $this->Db->get_rows($sql)) { $Member = new Member(); foreach($rows as $row) { $this->contact_list[] = new Member($row['network_member_id']); } } } Quote Link to comment Share on other sites More sharing options...
cstegner Posted February 26, 2007 Author Share Posted February 26, 2007 Jesirose and Barand, Don't mean to bring up a solved topic, but your explanation still left me feeling pretty...confused. If you think you could clear it up any better in my mind, I would greatly appreciate it. Thanks again. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 26, 2007 Share Posted February 26, 2007 Whenever you sort a set of items at some point you have to compare pairs of items to determine which one should should precede the other in the final output. EG. Suppose you want to sort C,B,A compare C~B C > B so swap them --> B,C,A compare C~A C > A so swap them --> B, A, C compare B~A B > A so swap them --> A, B, C compare B~C B < C so no swap --> A, B, C If you just have a simple array of strings or numbers then PHP knows how to compare the pairs of items to determine which should sort before the other. However, if you want to sort a more complex array, or sort in a non-conventional way (eg sort odd numbers before even numbers) then you need to define how the items should be compared. To do this you define your own sort function which accepts 2 array items as its arguments. When PHP is sorting it passes the pairs of items to this function,which returns -1, 0 or +1 depending on your rules for sorting them. 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.