SReject commented on a Page, Sort Array of IRC Nicks With Modes  -  Apr 10, 2014

By using the 'indexOf' method for strings you could shorten your code quite a bit:

function sortNames(names) {
    names.sort(function(name1, name2){
        var nameA = [
            "+%&@~".indexOf(name1[0]), 
            name1.toLowerCase().replace(/^[~@&%+]+/,"")
        ], nameB = [
            "+%&@~".indexOf(name2[0]), 
            name2.toLowerCase().replace(/^[~@&%+]+/,"")
        ];
        if (nameA[0] > nameB[0]) return -1;
        if (nameA[0] < nameB[0]) return 1;

        if (nameA[1] < nameB[1]) return -1;
        if (nameA[1] > nameB[1]) return 1;
        return 0  
    });
    return names;
}

General Thoughts/Suggestions:
1: For something like the userlist, I'd create a custom object to keep track of not just nicks and prefixes but also user hosts, modes, etc, and have a sort method in the prototype

2: Instead of hardcoding prefixes, I'd suggest pulling them from the 005 raw numeric. That way your code will work on any compliant server

PennyBreed  -  Apr 10, 2014

I like it. I've changed my snippet. Yes, my client does indeed store user information along with the nicks, and modes are parsed from 005, but it was not applicable here.

SReject  -  Apr 10, 2014

Nice, though I suggest moving the mode and regex var outside the sort function, and not using push(), since the values are statically placed:

It'd be more efficient/optimized to just hard code the array-filling instead of using push(). For small channels its not that noticable, but in large rooms of 1000+ users, the 'stalling spikes' while sorting the nicklist each time a user enters or changes nick would be quite noticable.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.