Alphabetically sort a drop down list

$managers = $this->Job->Manager->find( 'list');

When I display my job managers doing it this way will do it in alphabetical order. I needed to add a condition though.

$managers = $this->Job->Manager->find( 'list', array('conditions' => array('enabled' => 1)));

I needed to add the condition so it was only retrieving job managers whose accounts were enabled and after that it organized the list in order of their primary key.

Is there any way to have this condition while still keeping the list in alphabetic order to make it easy to find names?

Without seeing your full code it will be hard to see exactly what you are doing. I would imagine that your using MySQL in which case just order your mysql query.

I assume that somewhere after this code you actually generate a dropdown list.
What do you do that with?

If you first make an hash, sorry, associatve array of the names and the job/manager/thingies.
Somewhere in the objects you retrieve there is no doubt some getter-function with which you can get the name in the object.
[php]
$assarr = array();
foreach ( $managers as $manager )
{ $assarr[$manager->getName()] = $manager;
}
[/php]
and then you sort the associative array by its keys ( the names )
[php]
ksort( $assarr );
[/php]
after that you use your associative arrays order to show the managers/job/thingies in the right order
No idea how you do that, but let’s say you do with method ‘render’:
[php]
foreach ( $asarr as $name, $object )
{ $object->render();
}
[/php]

Something like this?

O.

Sponsor our Newsletter | Privacy Policy | Terms of Service