C#C
C#3y ago
olleeee

✅ sort list after property

Hi, im working with a WPF application and trying to do like a candy calculator. I can make everything and put in values, but now im trying to sort out the sorting of the list after a property. So i have done this:
 public class Person
 {
         public int Age { get; set; }
         public string FirstName { get; set; }
         public string LastName { get; set; }
         public int Candies { get; set; }

     public List<Person> PeopleList { get; set; }


     public override string ToString()
     {
         return $"{FirstName} {LastName}, Age: {Age}, Candies {Candies}";
     }

     public void DistrubuteCandyByAge()
     {

          PeopleList = PeopleList.OrderBy(x => x.Age).ToList();
     }

     public void DistrubuteCandyByFirstname()
     {
          PeopleList = PeopleList.OrderBy(x => x.FirstName).ToList();

     }
     public void DistrubuteCandyByLastName()
     {
          PeopleList = PeopleList.OrderBy(x => x.LastName).ToList();
     }
Was this page helpful?