Index: author-template.php
===================================================================
--- author-template.php	(revision 12704)
+++ author-template.php	(working copy)
@@ -247,6 +247,11 @@
  * or as a string.</li>
  * <li>html (bool) (true): Whether to list the items in html for or plaintext.
  * </li>
+ * <li>orderby (string) ('name'): The sort order of the list.</li>
+ * <li>order (string) ('ASC'): Sort order for authors (either ascending or
+ * descending).</li>
+ * <li>number (int) (NULL): Sets the number of authors to display. This
+ * causes the SQL LIMIT value to be defined. Default to no LIMIT.</li>
  * </ul>
  *
  * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
@@ -255,46 +260,72 @@
  * @return null|string The output, if echo is set to false.
  */
 function wp_list_authors($args = '') {
-	global $wpdb;
+	global $wpdb, $blog_id;
 
 	$defaults = array(
 		'optioncount' => false, 'exclude_admin' => true,
 		'show_fullname' => false, 'hide_empty' => true,
 		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
-		'style' => 'list', 'html' => true
+		'style' => 'list', 'html' => true, 'orderby' => 'name',
+		'order' => 'ASC', 'number' => NULL
 	);
 
 	$r = wp_parse_args( $args, $defaults );
 	extract($r, EXTR_SKIP);
 	$return = '';
 
-	/** @todo Move select to get_authors(). */
-	$users = get_users_of_blog();
-	$author_ids = array();
-	foreach ( (array) $users as $user )
-		$author_ids[] = $user->user_id;
-	if ( count($author_ids) > 0  ) {
-		$author_ids = implode(',', $author_ids );
-		$authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
+	if ( $show_fullname ) {
+		$author_name_field = "CASE
+		WHEN TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) <> ''
+		THEN
+			TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, '')))
+		ELSE
+			$wpdb->users.display_name
+		END";
+		$author_name_join = "
+		LEFT JOIN $wpdb->usermeta AS meta_first_name ON meta_first_name.user_id = $wpdb->users.ID AND meta_first_name.meta_key = 'first_name'
+		LEFT JOIN $wpdb->usermeta AS meta_last_name ON meta_last_name.user_id = $wpdb->users.ID AND meta_last_name.meta_key = 'last_name'
+		";
 	} else {
-		$authors = array();
+		$author_name_field = "$wpdb->users.display_name";
+		$author_name_join = "";
 	}
 
-	$author_count = array();
-	foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
-		$author_count[$row->post_author] = $row->count;
+	if ( $orderby == 'name' ) {
+		$author_sort = "ORDER BY author_name $order";
+	} else {
+		$author_sort = "ORDER BY author_count $order";
+	}
 
+	$number = intval( $number );
+	if ( $number > 0 ) {
+		$author_limit = "LIMIT $number";
+	} else {
+		$author_limit = '';
+	}
+
+	$blog_prefix = $wpdb->get_blog_prefix($blog_id);
+	$authors = $wpdb->get_results("
+		SELECT $wpdb->users.ID, $wpdb->users.user_nicename, COUNT($wpdb->posts.ID) as author_count, $author_name_field as author_name
+		FROM $wpdb->users
+		" . ( !$hide_empty ? "LEFT " : '' ) . "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID
+		$author_name_join
+		LEFT JOIN $wpdb->usermeta AS meta_capabilities ON meta_capabilities.user_id = $wpdb->users.ID AND meta_capabilities.meta_key = '{$blog_prefix}capabilities'
+		WHERE ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL)
+		AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private' OR $wpdb->posts.post_status IS NULL) " .
+		( $exclude_admin ? "AND $wpdb->users.user_login <> 'admin' " : '' ) . "
+		GROUP BY $wpdb->users.ID
+		$author_sort
+		$author_limit
+		");
+
 	foreach ( (array) $authors as $author ) {
 
 		$link = '';
 
-		$author = get_userdata( $author->ID );
-		$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
-		$name = $author->display_name;
+		$posts = $author->author_count;
+		$name = $author->author_name;
 
-		if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
-			$name = "$author->first_name $author->last_name";
-
 		if( !$html ) {
 			if ( $posts == 0 ) {
 				if ( ! $hide_empty )
@@ -312,7 +343,7 @@
 			if ( ! $hide_empty )
 				$link = $name;
 		} else {
-			$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
+			$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>';
 
 			if ( (! empty($feed_image)) || (! empty($feed)) ) {
 				$link .= ' ';

