So a while ago I posted a hideous, but working, example of what I was using as my leaderboard script. Having had a few months learning more about code, I have come up with a FAR more elegant solution to the problem! Enter Linq!

Essentially what we’re going to do is have a List<> of KeyValuePair’s. I’m sure there are a few other ways of doing this, however this is the one that I found works best for me.

Normally I would just use a standard dictionary for this, but there is a small chance that a duplicate score will come up which will end up breaking everything. So creating a list solves our problem because we can have as many duplicate scores as we want and everything will be fine 🙂 So to keep our scores and our names together we are going to create a
List <KeyValuePair <int, string>>

In this example I’m creating a list of scores to show how it would be laid out, but all you have to do is grab the leaderboard from wherever you have it saved and you should be good to go.

using System.Linq;
using System.Collections.Generic;

public class LeaderboardClass {

	void Scores () {

		// this is what we would pass through from whatever we're calling from
		int score = 180000;
		string name = "DEREK";
		// if we're just passing through an int and string we create a key value pair from the values
		KeyValuePair <int, string> newScore = new KeyValuePair <int, string> (score, name);


		// we would grab this from wherever our leaderboard is saved
		// in this case we are using a list of KeyValuePairs because serializing dictionairies is a bitch
		// all we have to do is add our newScore to the end of this list
		List <KeyValuePair <int, string>> leaderboard = new List <KeyValuePair <int, string>> {
			new KeyValuePair <int, string> (154000, "PATRICIA"),
			new KeyValuePair <int, string> (200053, "DOUGLAS"),
			new KeyValuePair <int, string> (150000, "SAMUEL"),
			new KeyValuePair <int, string> (220000, "ROSANNA"),
			new KeyValuePair <int, string> (169000, "BELLA"),
			newScore
		};

		foreach (KeyValuePair<int, string> pair in leaderboard)
			Debug.Log (string.Format ("{0} | {1}", pair.Value, pair.Key));

		// Printing them out currently would give us this;

		// PATRICIA | 154000
		// DOUGLAS | 200053
		// SAMUEL | 150000
		// ROSANNA | 220000
		// BELLA | 169000
		// DEREK | 180000	<--- this is our newScore

	}
}

So we have our leaderboard set up. Great! But it’s not in order.. My solution? Use Linq’s rather helpful tool: orderby.

In my example we are passing the aforementioned ‘un-ordered leaderboard’ into our function.


void SomeMethod () {
	...
	LeaderBoard (leaderboard);
}

void LeaderBoard (List<KeyValuePair <int, string>> _playerscores) {

	// Using Linq we check each score and organise them from highest to lowest
	// We find the pairs in our list, and check the 'Key' as our variable to order
	var ordered = 
		from pair in _playerscores
		orderby	pair.Key descending
	    select pair;
	
	foreach (KeyValuePair<int, string> pair in ordered)
		Debug.Log (string.Format ("{0} | {1}", pair.Value, pair.Key));

	// Printing them out now would give us our ordered list;

	// ROSANNA | 220000
	// DOUGLAS | 200053
	// DEREK | 180000	<--- this is our newScore
	// BELLA | 169000
	// PATRICIA | 154000
	// SAMUEL | 150000

}

And they we go! A working, ordering, and elegant (in my opinion) leaderboard script!

Advertisements