Sort class list by integer

PHOTO EMBED

Fri Nov 05 2021 17:58:43 GMT+0000 (UTC)

Saved by @miguel9177 #c#

   class ScoreBoard
    {
        // Constructor that takes points and number of records, this constructor will be called if the user doesnt insert the name, and will assing the name to unknown
        public ScoreBoard(int points, int numberOfRecords)
        {
            
            playerName = "unknown";
            playerPoints = points;
            numberOfSaves = numberOfRecords + 1;
            
        }

        // Constructor that takes all the arguments, this constructor will set the name, points and number of records
        public ScoreBoard(string name, int points, int numberOfRecords)
        {
            playerName = name;
            playerPoints = points;
            numberOfSaves = numberOfRecords +1;
        }

        //Create a read and set variable called player name that will save the player name
        public string playerName { get; set; }

        //Create a read and set variable called Player points that will save the player points
        public int playerPoints { get; set; }

        //Create a read only variable called number of saves
        public int numberOfSaves { get; }        
    }
    
//this will save all the player scores
    List<ScoreBoard> allPlayerScores = new List<ScoreBoard>(); 		


void sortListScoreboard()
{ //do a sorting loop
        for (int i = 0; i < allPlayerScores.Count - 1; i++)
        {
            //this loop will check every item with the items from the other for
            for (int x = 0; x < allPlayerScores.Count - 1; x++)
            {
                //check if the item is bigger then this item, if it is just move it up
                if(allPlayerScores[i].playerPoints>allPlayerScores[x].playerPoints)
                {
                    int temp = allPlayerScores[x].playerPoints;
                    string tempName = allPlayerScores[x].playerName;
                    allPlayerScores[x].playerPoints = allPlayerScores[i].playerPoints;
                    allPlayerScores[i].playerPoints = temp;
                    allPlayerScores[x].playerName = allPlayerScores[i].playerName;
                    allPlayerScores[i].playerName = tempName;
                }
            }
            
        }
}
content_copyCOPY

this will sort a class by a class member in this case player points