Jump to content

How to put an array of objects into alphabetical order???


Recommended Posts

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?

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');
?>

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()

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.

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.

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']);
		}
	}
}

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.

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.

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.