Sort a list <t> by a property in the object <t>

PHOTO EMBED

Wed May 21 2025 16:36:08 GMT+0000 (Coordinated Universal Time)

Saved by @dhfinch #c#

// creates duplicate list in ascending order
List<WindRosePlotDataRecord> SortedList = _Rows.OrderByDescending(o => o.Parameter).ToList(); 

// creates duplicate list in descending order
List<WindRosePlotDataRecord> SortedList = _Rows.OrderByDescending(o => o.Parameter).ToList(); 
            
// sort-in-place in ascending order
_Rows.Sort((x, y) => x.Parameter.CompareTo(y.Parameter));  

// sort-in-place in descending order
_Rows.Sort((x, y) => y.Parameter.CompareTo(x.Parameter));  // we switched x and y in CompareTo
content_copyCOPY

https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object