Searching for users from a plugin I know how to find user by username and email. The problem comes when I need to query users based on a custom field. 

Here is my scenario. I have about 2,000 users (part of a user group 'Affiliates') that I have created in our system. They have many custom fields including affiliateId and studentCount. I need to be able to execute two separate queries. 


Find a single user by his/her affiliateId (int).
Query all users within the Affiliate group, order by studentCount asc and return the top record. (I need a user that has the fewest number of students).


I have searched docs and tried numerous combinations to query users. Any help at this point greatly appreciated. Is search the best way? If so, how do you order the search result?
Find a single user by his/her affiliateId (int).


$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;affiliateId = 1;
$users = $criteria-&gt;find();



  Query all users within the Affiliate group, order by studentCount asc and return the top record. (I need a user that has the fewest number of students).


$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;affiliateGroup = 'myGroup';
$criteria-&gt;order = 'studentCount ASC';
$criteria-&gt;limit = 1;
$users = $criteria-&gt;find();