Wordpress - Need help sorting "My Sites" Alphabetically

Easy one.

<?php
/*
Plugin Name: Sort My-Sites
Description: Sorts the My Sites listing on both the page and in the 3.3 admin bar dropdown
Author: Otto
*/

add_filter('get_blogs_of_user','sort_my_sites');
function sort_my_sites($blogs) {
        $f = create_function('$a,$b','return strcasecmp($a->blogname,$b->blogname);');
        uasort($blogs, $f);
        return $blogs;
}

Edit: If you want a PHP 7 version:

add_filter('get_blogs_of_user', function( $blogs ) {
    uasort( $blogs, function( $a, $b ) { 
        return strcasecmp( $a->blogname, $b->blogname );
    });
    return $blogs;
});

This plugin does the job too:

Reorder My Sites

For WordPress Multisite. Reorders the My Sites dropdown menu in the Admin Bar alphabetically. It keeps the main blog at the top.

Tags:

Multisite