C
C#12mo 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();
}
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();
}
2 Replies
olleeee
olleeeeOP12mo ago
BlazeBin - hltvtmjtpazw
A tool for sharing your source code with the world!
JP
JP12mo ago
👀 It appears like you provided a lot of good context, without a question
Want results from more Discord servers?
Add your server