Answers for "list sort by name c#"

C#
10

c# how to sort a list

var simpleList = new List<ObjectName>();
// Order by a property
list.OrderBy(o => o.PropertyName);
// Order by multiple property
list.OrderBy(o => o.PropertyName).ThenBy(o => o.AnotherProperty);
// Same but descending
list.OrderByDescending(o => o.PropertyName).ThenByDescending(o => o.AnotherProperty);
Posted by: Guest on May-29-2020
3

list sorting c#

using System.Linq;

//sorts in acending order {1,2,3,4,5}
myList.Sort();

//Sorts in decending order {5,4,3,2,1}
myList.Sort();
myList.Reverse();

//to sort a custom list of classes you have to 
//add this inside of your class
public int CompareTo(MyClass other)//replace MyClass with your class type
{
    //replace varInClass to the variable that you would like to compare
	return this.totalScore.CompareTo(other.varInClass)
}
Posted by: Guest on December-14-2020
0

c# list object sort alphabetically

using System.Linq;

List<MyObject> list = new List<MyObject>()
{ new MyObject("Peterson", "Mike"), new MyObject("Johnson", "Tim") };

list.OrderBy((item) => item.LastName);
// In this example, the list would be arranged like so: Johnson, Peterson
Posted by: Guest on September-14-2021

C# Answers by Framework

Browse Popular Code Answers by Language