Sorting a List Using Lambda/Linq to Objects

List T .Sort using Lambda expression with 2 parameters

You can't tell from the method signature how many times the argument will be used. I could write a method Foo(Func<string, string>) that never called its argument.

However, since this is a sorting method, it's not enough to iterate once over the list. There are lots of different sorting algorithms, but they tend to use O(n log(n)) comparisons. In other words, doubling the length of the list sorted will result in slightly more than double the calls to your lambda function.

If you want to see what actually happens when it's running, add some logging!

films.Sort((f1, f2) => 
{
Console.WriteLine("Comparing ${f1.Name} to ${f2.Name}");
return f1.Name.CompareTo(f2.Name);
});

How to sort same list using lambda expression?

You must add .ToList() at the because OrderBy returns an IEnumerable, so you try as:

ObjectList = ObjectList.OrderBy(x=>x.Name). ToList() ;

Lambda expression for Order List of collection of objects based on a property of the object in C#

It can be done as follows:

listAll = listAll.OrderBy(e => Array.IndexOf(keys, e.FirstOrDefault()?.key))
.ToList();

dotnetfiddle

To Order By Descending sub-elements using LINQ or Lambda Expression

You enumerate result each time that you enumerate the return IEnumerable of DoStuff, plus one additional time inside the loop in DoStuff itself. Modifications you make inside the loop do not stay, though, because of deferred execution: next time you enumerate DoStuff() there is another call to DoOtherStuff etc.

You can fix this in several ways:

  • Convert result to list before sorting children, i.e.

    DoOtherStuff() // Returns IEnumerable<Parent>
    .OrderByDescending(SomePredicate)
    .ThenBy(AnotherPredicate)
    .ToList();
  • Add child sorting in a Select:

    DoOtherStuff() // Returns IEnumerable<Parent>
    .Select(parent => {
    parent.Children = parent.Children.OrderBy(YetAnotherPredicate).ToList();
    return parent;
    })
    .OrderByDescending(SomePredicate)
    .ThenBy(AnotherPredicate)
    .ToList();
  • Use yield return result in the loop (this is a variation of the Select solution).

Lambda expression for Order List of collection of objects based on a property of the object in C#

It can be done as follows:

listAll = listAll.OrderBy(e => Array.IndexOf(keys, e.FirstOrDefault()?.key))
.ToList();

dotnetfiddle

Sort List Object via Lambda expressions

The simplest way is for Person and Employee to share some common base class or implement a common interface. For example:

public interface IName
{
string Name { get; }
}

class Person : IName
{
public string Name { get; set; }
public string Car { get; set; }
}

class Employee : IName
{
public string Name { get; set;}
public string Salary { get; set;}
}

And then instead of creating a List<object> and using OfType, you could create your list using this common type:

var people = new List<IName>();  
people.AddRange(_ListPerson);
people.AddRange(_ListEmployee);

And group:

var grouped = people.GroupBy(x => x.Name);

Though you mention you want to sort, which would actually be achieved using OrderBy or OrderByDescending:

var sorted = people.OrderBy(x => x.Name);

Sorting a list alphabetically with lambda after a certain position

Try using this:

list.Take(3).Concat(list.Skip (3).OrderBy (x => x.Name))


Related Topics



Leave a reply



Submit