Joshua Faust Joshua Faust - 2 months ago 18
C# Question

Compare and calculate first, second, and third place

I am trying to figure out the most efficient way to calculate first, second, and third place for a simple C# program in which the purpose is to find the winner, 2nd place, and 3rd place and show their names accordingly however, my code seems way to large for such a simple task. I am new and I an using If statements to complete the required calculation but, I know there is a better way. Can someone enlighten me?

Here is my current code and where I stopped after realizing the amount a code this is going to take.

private void calculateButton_Click(object sender, EventArgs e)
{
// Define Name and Time Variables
string runnerone = runnerOneNameTextBox.Text; // Runner One Name
string runnertwo = runnerTwoNameTextBox.Text; // Runner Two Name
string runnerthree = runnerThreeNameTextBox.Text; // Runner Three Name
double runnerOneTime = double.Parse(runnerOneTimeTextBox.Text); // Runner One Time
double runnerTwoTime = double.Parse(runnerTwoTimeTextBox.Text); // Runner Two Time
double runnerThreeTime = double.Parse(runnerThreeTimeTextBox.Text); // Runner Three Time

//-------------------------------------------------------------------------
// Start of the If statement to calculate who is first, second, and third.
//-------------------------------------------------------------------------

// FIRST PLACE CODE:
if (runnerOneTime > runnerTwoTime && runnerOneTime > runnerThreeTime) // Runner One is greater than everyone
{
firstPlaceLabel.Text = runnerOneNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerOneNameTextBox.Text;
}
else if (runnerOneTime == runnerTwoTime && runnerOneTime > runnerThreeTime) // Runner one is equal to runner two
{
firstPlaceLabel.Text = runnerOneNameTextBox.Text;
firstPlaceLabel.Text = runnerTwoNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerOneNameTextBox.Text;
firstPlaceTrophyLabel.Text = runnerTwoNameTextBox.Text;
}
else if (runnerOneTime > runnerTwoTime && runnerOneTime == runnerThreeTime)
}
}


}

Answer

You have a list of three runners, so let the .NET list sorting functionality come to your rescue:

private class RunnersAndTimes
{
    public string Name {get};
    public double Time {get};

    public RunnersAndTimes(string name, double time)
    {
        Time = time;
        Name = name;
    }
}
...
private void calculateButton_Click(object sender, EventArgs e)
{
    var runnersAndTimes = new List<RunnersAndTimes> {
        new RunnersAndTimes(runnerOneNameTextBox.Text, 
                            double.Parse(runnerOneTimeTextBox.Text)),
        new RunnersAndTimes(runnerTwoNameTextBox.Text, 
                            double.Parse(runnerTwoTimeTextBox.Text)),
        new RunnersAndTimes(runnerThreeNameTextBox.Text, 
                            double.Parse(runnerThreeTimeTextBox.Text))
    };

    var orderedRunners = runnersAndTimes.OrderBy(runner => runner.Time).ToList();

    firstPlaceLabel.Text = orderedRunners[0];
    secondPlaceLabel.Text = orderedRunners[1];
    thirdPlaceLabel.Text = orderedRunners[2];
}
Comments