How to sort the general list?

I have a general list ...

public List <ApprovalEventDto> ApprovalEvents

The EventDto statement has

public class ApprovalEventDto  
{
    public string Event { get; set; }
    public DateTime EventDate { get; set; }
}

How to sort the list by event date?

+3
source share
7 answers

You can use List.Sort () as follows:

ApprovalEvents.Sort((lhs, rhs) => (lhs.EventDate.CompareTo(rhs.EventDate)));
+11
source
using System.Linq;

void List<ApprovalEventDto> sort(List<ApprovalEventDto> list)
 { return list.OrderBy(x => x.EventDate).ToList();
 }
+5
source
ApprovalEvents.Sort((x, y) => { return x.EventDate.CompareTo(y.EventDate); });
+3

, .NET 3.5, OrderBy, marxidad. , List.Sort.

List.Sort , IComparer - , , .

MiscUtil ProjectionComparer, ( OrderBy), CompareTo . , , , . ( MiscUtil. . .)

+1

Darksiders, :

ApprovalEvents.Sort((a, b) => (a.EventDate.CompareTo(b.EventDate)));
0

. . WikiPedia, n-Log n-, .

For certain types of data, you can also use a pigeon to get order n due to more memory usage.

-1
source

You can use the List.Sort () method with an anonymous method or a lambda expression. More on MSDN

-1
source

Source: https://habr.com/ru/post/1697965/


All Articles