How to sort generic list C#?

I need to sort a list that has temperatures stored in it.

This defines the list:
private List<PollenCountTemp> _pollenCountTemps = new List<PollenCountTemp>();

And this is the method used to get the temperatures:
public void Fill()
        {
            string getPollenHigh = ""; //pollen high count
            string getPollenLow = ""; // pollen low count
            string Temperature = "";


            //setup variables for gathering information
            //Start gathering info from user

            Console.WriteLine("Enter reported temperatures...");
            Console.WriteLine("\n");
            foreach (WeatherStation station in _weatherStations)
            {
                Console.Write(station.StationName);
                Console.WriteLine("\n");
                Console.Write(station.ContactName);
                Console.WriteLine("\n");
                Console.WriteLine("Enter Temperature:");

                Temperature = Console.ReadLine(); // Get string from user
                if (Temperature != null)
                {
                    if (Temperature.Equals("Stop"))
                    {
                        break;
                    }
                    else
                    {

                        Console.WriteLine("\n");
                        Console.WriteLine("Enter Pollen Count:");
                        Console.WriteLine("\n");
                        Console.WriteLine("Enter Pollen High:");
                        getPollenHigh = Console.ReadLine();
                        if (getPollenHigh.Equals("stop"))
                            break;

                        Console.WriteLine("\n");

                        Console.WriteLine("Enter Pollen Low:");
                        getPollenLow = Console.ReadLine();
                        if (getPollenLow.Equals("stop"))
                            break;

                        // Convert it to a number...
                        station.PollenInfo.Temperature = double.Parse(Temperature);

                        // get Pollen high count
                        station.PollenInfo.HighPollenCount = double.Parse(getPollenHigh);


                        // get Pollen low count
                        station.PollenInfo.LowPollenCount = double.Parse(getPollenLow);

                        Console.WriteLine("\n");
                        Console.WriteLine("==============================================");
                    }
                }
            }



LVL 7
MHenryAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
p_davisConnect With a Mentor Commented:
List<PollenCountTemp> sortedList = (from p in  _pollenCountTemps
order by p.Temperature desc
select p).ToList();


sorry about the last post -- don't know how that happened.
0
 
p_davisCommented:
how do you wnat it sorted?


_pollenCountTemps.Sort(delegate(PollenCountTemp pct1, PollenCountTemp pct)
{
    return pct1.Temperature.CompareTo(pct2.Temperator);

});

will do a simple sort

you can also use linq
0
 
MHenryAuthor Commented:
I need it sorted high to low. I also need to know how to use console.writeline to print out high and low.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
p_davisCommented:
List<PollenCountTemp> sortedList = (from p in _pollenCountTemps)
_pollenCountTemps = new List<PollenCountTemp>();
0
 
MHenryAuthor Commented:
That's showing this error at the end of the first line

a query body must end with a select clause or a group clause?
0
 
p_davisCommented:
desc should be descending and order by shouldn't have a space
0
 
p_davisCommented:
you should also add System.Linq to your usings.
0
 
MHenryAuthor Commented:
Looks like it's working. How would I print it out? Like this?

Console.WriteLine(_pollenCountTemps[0];
            Console.WriteLine(_pollenCountTemps[8];

Is there a better way to find the end?

0
 
p_davisCommented:
then for max you can use

Console.WriteLine(sortedList.Max(x=>x.Temperature).ToString());
Console.WriteLine((sortedList.Min(x=>x.Temperature).ToString());
0
 
MHenryAuthor Commented:
thanks!
0
All Courses

From novice to tech pro — start learning today.