Sort those Twits

If you haphazardly make code because of some nonsensical idea to create something (anything) everyday, then you are bound to end up with some ugly mess of JavaScript, which is what happened to me when I took a look at my text file today. I made a fatal error in continuously streaming inputs to a text file using the command fs.appendFile('usertest.txt',JSON.stringify(userList));. I had a plan to quickly read this in via a quick JSON.parse(data) on the contents of usertest.txt; however, writing hundreds of strings is not that same as writing a coherent JSON file.

Regardless, I was able to read in my usernames, which all had a value of 1 anyway) by some clever regex expressions and modification:

UserRaw = data.split(/":1}{"/g);
userRaw[0] = userRaw[0].substr(2);
userRaw[userRaw.length-1] = userRaw[userRaw.length-1].substr(0,userRaw[userRaw.length-1].length-4);

This got me a dictionary of the users and how many tweets they made while I was watching, but that still isn't very useful information. I then decided to run through the data to count how many tweets for each user:

for (i=0;i<userRaw.length;i++) {
    if (userList[userRaw[i]]) {
        userList[userRaw[i]]++;

    } else {
        userList[userRaw[i]] = 1;
        totalUsers++;
    }
}

Now, armed with a list of users and how many tweets each made about bikes, it was time to implement the famous quicksort algorithm. I had built this before, and it is implemented in most languages, but I wanted to make sure I had this skill down. There is probably a straightforward way to sort out your dictionary in JavaScript, and writing all of this to a sql database would be much more efficient and logical, but as long as I am creating a plethora of projects for no reason, then I mineaswell put my head down and hammer out my own algorithms sometimes. There is no point in posting up my quicksort code, but there was a quick change to determine when only the value was required or the entire list item should be swapped.

The final piece was to make the function at least slightly faster, which meant eliminating all of the people who only tweeted once before implementing quicksort:

var sorter = function(userList) {
    var oneTweets = [];
    var moreTweets = [];
    for (user in userList) {
        if (userList[user] == 1) {
            oneTweets.push({key : user, value: userList[user]});

        } else {
            moreTweets.push({key : user, value: userList[user]});
        }
    }
    quicksort(moreTweets,0,moreTweets.length-1);
    console.log(moreTweets);
}

What do you know, @BestJobInfo was tearing it up last night with 49 tweets about 'bike'! I think this project might evolve into something more useful if I don't get too bored.