Sorting an IList in C#

前端 未结 15 2341
臣服心动
臣服心动 2020-11-28 06:36

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.

Turns out the IList

15条回答
  •  抹茶落季
    2020-11-28 07:10

    try this  **USE ORDER BY** :
    
       public class Employee
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
    
     private static IList GetItems()
            {
                List lst = new List();
    
                lst.Add(new Employee { Id = "1", Name = "Emp1" });
                lst.Add(new Employee { Id = "2", Name = "Emp2" });
                lst.Add(new Employee { Id = "7", Name = "Emp7" });
                lst.Add(new Employee { Id = "4", Name = "Emp4" });
                lst.Add(new Employee { Id = "5", Name = "Emp5" });
                lst.Add(new Employee { Id = "6", Name = "Emp6" });
                lst.Add(new Employee { Id = "3", Name = "Emp3" });
    
                return lst;
            }
    
    **var lst = GetItems().AsEnumerable();
    
                var orderedLst = lst.OrderBy(t => t.Id).ToList();
    
                orderedLst.ForEach(emp => Console.WriteLine("Id - {0} Name -{1}", emp.Id, emp.Name));**
    

提交回复
热议问题