Local Leaderboard (Sort List) Help

Warning the below code is awful and inefficient. I am just trying to do this anyway possible.

I only want 10 scores shown. So I just used 10 GUITexts instead of creating them through code which would be a better solution. I just need to know how to compare these lists and put the scores back into the texts…

I don’t understand IComparable or IComparer. I have an idea of how it works but I can’t seem to figure out how to use it to do what I need it to do.

I’m not one to ask for a code solution usually but I am a bit stumped here.

using UnityEngine;
using System.Collections.Generic;

public class HiScoreShow : MonoBehaviour {
	public GUIText hiScoreHere;
	public GUIText hiScoreHere2;
	public GUIText hiScoreHere3;
	public GUIText hiScoreHere4;
	public GUIText hiScoreHere5;
	public GUIText hiScoreHere6;
	public GUIText hiScoreHere7;
	public GUIText hiScoreHere8;
	public GUIText hiScoreHere9;
	public GUIText hiScoreHere10;

	public static string curHigh;
	public static string curName;

	public static string curHigh2;
	public static string curName2;

	public static string curHigh3;
	public static string curName3;

	public static string curHigh4;
	public static string curName4;

	public static string curHigh5;
	public static string curName5;

	public static string curHigh6;
	public static string curName6;

	public static string curHigh7;
	public static string curName7;

	public static string curHigh8;
	public static string curName8;

	public static string curHigh9;
	public static string curName9;

	public static string curHigh10;
	public static string curName10;

	List<string> playerNameList = new List<string>();
	List<int> scoreList = new List<int>();

	// Use this for initialization
	void Start () {

		string playerNow = Score.playerNow;
		int score = Score.score;
		curHigh = PlayerPrefs.GetString ("HiScore", "500000");
		curHigh2 = PlayerPrefs.GetString ("HiScore2", "450000");
		curHigh3 = PlayerPrefs.GetString ("HiScore3", "400000");
		curHigh4 = PlayerPrefs.GetString ("HiScore4", "350000");
		curHigh5 = PlayerPrefs.GetString ("HiScore5", "300000");
		curHigh6 = PlayerPrefs.GetString ("HiScore6", "250000");
		curHigh7 = PlayerPrefs.GetString ("HiScore7", "200000");
		curHigh8 = PlayerPrefs.GetString ("HiScore8", "150000");
		curHigh9 = PlayerPrefs.GetString ("HiScore9", "100000");
		curHigh10 = PlayerPrefs.GetString ("HiScore10", "50000");


		scoreList.Add (curHigh);
		scoreList.Add (curHigh2);
		scoreList.Add (curHigh3);
		scoreList.Add (curHigh4);
		scoreList.Add (curHigh5);
		scoreList.Add (curHigh6);
		scoreList.Add (curHigh7);
		scoreList.Add (curHigh8);
		scoreList.Add (curHigh9);
		scoreList.Add (curHigh10);


		curName = PlayerPrefs.GetString ("BestPlayer", "Titan");
		curName2 = PlayerPrefs.GetString ("BestPlayer2", "Snoopy");
		curName3 = PlayerPrefs.GetString ("BestPlayer3", "Maverick");
		curName4 = PlayerPrefs.GetString ("BestPlayer4", "Shit Pickle");
		curName5 = PlayerPrefs.GetString ("BestPlayer5", "Felix");
		curName6 = PlayerPrefs.GetString ("BestPlayer6", "Alfred");
		curName7 = PlayerPrefs.GetString ("BestPlayer7", "Grenade");
		curName8 = PlayerPrefs.GetString ("BestPlayer8", "Nancy");
		curName9 = PlayerPrefs.GetString ("BestPlayer9", "Bob");
		curName10 = PlayerPrefs.GetString ("BestPlayer10", "Glass Joe");

		playerNameList.Add (curName);
		playerNameList.Add (curName2);
		playerNameList.Add (curName3);
		playerNameList.Add (curName4);
		playerNameList.Add (curName5);
		playerNameList.Add (curName6);
		playerNameList.Add (curName7);
		playerNameList.Add (curName8);
		playerNameList.Add (curName9);
		playerNameList.Add (curName10);


	//	if (score > int.Parse(HiScore.hiScore)) {
	//		playerNow = PlayerPrefs.GetString("PlayerName", "Player 1");
	//		PlayerPrefs.SetString("HiScore", score.ToString());
	//		PlayerPrefs.SetString("BestPlayer", playerNow);
	//	}

		hiScoreHere.text = "1. " + curName + " : " + curHigh;
		hiScoreHere2.text = "2. " + curName2 + " : " + curHigh2;
		hiScoreHere3.text = "3. " + curName3 + " : " + curHigh3;
		hiScoreHere4.text = "4. " + curName4 + " : " + curHigh4;
		hiScoreHere5.text = "5. " + curName5 + " : " + curHigh5;
		hiScoreHere6.text = "6. " + curName6 + " : " + curHigh6;
		hiScoreHere7.text = "7. " + curName7 + " : " + curHigh7;
		hiScoreHere8.text = "8. " + curName8 + " : " + curHigh8;
		hiScoreHere9.text = "9. " + curName9 + " : " + curHigh9;
		hiScoreHere10.text = "10. " + curName10 + " : " + curHigh10;


	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Check out this forum post, it’s got a pretty good example on numerical sorting:

will this work with the names too though? I think I need the list to have both name and score as one item. Then sort only the score int and not the Name string…

Yes - I went with the assumption that your current code isn’t optimized, like you said. Ideally you would be using a dictionary or a custom class to store the scoring data, which would allow both a score and a name, and sort from there.