loading
open in
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;

class MainClass {
  public static void Main (string[] args) {
    
    var personList = new List<Person>
    {
      new Person {Name="Fatih", Age=31 },
      new Person {Name ="Alice", Age=2 },
      new Person {Name="Mehmet", Age=30 },
      new Person {Name="Eric", Age = 28  },
      new Person {Name="Tarik", Age = 22  }
    };

    var sortByAgeAsc = (Comparison<Person>)((x, y) =>
    {
      int result = x.Age.CompareTo(y.Age);

      return result;
    });

    var sortByAgeDesc = (Comparison<Person>)((x, y) =>
    {
      int result = y.Age.CompareTo(x.Age);

      return result;
    });

    var sort = new Func<string, Comparison<Person>>(f =>
    {
      if (f == "Asc") return sortByAgeAsc;
      else if (f == "Desc") return sortByAgeDesc;
      return null;
    });

    personList.Sort(sort("Desc"));

    foreach(Person person in personList)
    {
      Console.WriteLine($"{person.Name} {person.Age}");
    }
  }

  public class Person
  {
    public string Name { get; set; }
    public int Age { get; set; }
  }
}