private void UpdatePlayerScoresArray() { Array.Sort(playerScores); // Even though the aray *should* be sorted, we sort here incase it was initialized out-of-order via the property inspector ScoreElement newScore = new ScoreElement(PlayerPrefs.GetString("PlayerName", GameManager.Instance.defaultPlayerName), Time.timeSinceLevelLoad - startTimeOffset, currentLevelScore); float newScoreRank = newScore.CalculateScoreRank(); for (int i = 0; i < playerScores.Length; i++) { bool hasInserted = false; float currentScoreRank = playerScores[i].CalculateScoreRank(); if (newScoreRank > currentScoreRank) { hasInserted = true; currentPlayerScoresEntryIndex = i; for (int j = i; j < playerScores.Length; j++) { ScoreElement temp = playerScores[j]; playerScores[j] = newScore; newScore = temp; } } if (hasInserted) // If we've inserted the new element, we're done { break; } } // End score insertion loop SaveScores(); }
// Compares this object with the received object: // If this.score > otherScore, return 1 // If this.score == otherScore, return 0 // If this.score < otherScore, return -1 public int CompareTo(object obj) { if (obj == null) { return(1); } ScoreElement otherScore = (ScoreElement)obj; return((int)Mathf.Sign(otherScore.CalculateScoreRank() - this.CalculateScoreRank())); // Rank is calculated as: points - (time in seconds) }