Ordering results for form autocomplete fields I've done the cursory googling, but so far I haven't found anything close to an answer for this.
I'm using the Masquerade module with a custom module on top to extend it and make its searching algorithms a bit more &quot;clever&quot; (Soundexes, custom fields and such), and it returns the results in a certain order, passed through to drupal_json_output(). However, because JSON isn't exactly indexed, it means that when the autocomplete text field picks it up, it's in a slightly screwy order based on its keys (which are the users' usernames). I can forcibly send a array_values() to the JSON output, but it breaks Masquerade's functionality as it requires the username to be the key. I'd imagine this would be a problem with other autocompletion stuff in Drupal that uses JSON loads.
Can anyone suggest a way of doing this please? I'm not sure whether it's a matter of Masquerade or of D7's textfield autocompletion.
Thanks
I would think its the query that handles the ordering. You can alter the query that fetches the results for the autocomplete field. Find the query in the module code and it should have a tag associated with it. You can then alter the query by implementing hook_query_TAG_alter

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_query_TAG_alter/7

This will allow you to change the orderby for the query

EDIT: Looking at the masquerade module I don't think it adds tags to the queries which means you cant mess with the ordering.