@kenanhancer/

CSharp_Comparison

C#

No description

fork
loading
Files
  • main.cs
  • main.exe

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.11512949736768507","path":"main.cs","file":{"path":"main.cs","content":{"asEncoding":{"base64":"dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKCmNsYXNzIE1haW5DbGFzcyB7CiAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4gKHN0cmluZ1tdIGFyZ3MpIHsKICAgIAogICAgdmFyIHBlcnNvbkxpc3QgPSBuZXcgTGlzdDxQZXJzb24+CiAgICB7CiAgICAgIG5ldyBQZXJzb24ge05hbWU9IkZhdGloIiwgQWdlPTMxIH0sCiAgICAgIG5ldyBQZXJzb24ge05hbWUgPSJBbGljZSIsIEFnZT0yIH0sCiAgICAgIG5ldyBQZXJzb24ge05hbWU9Ik1laG1ldCIsIEFnZT0zMCB9LAogICAgICBuZXcgUGVyc29uIHtOYW1lPSJFcmljIiwgQWdlID0gMjggIH0sCiAgICAgIG5ldyBQZXJzb24ge05hbWU9IlRhcmlrIiwgQWdlID0gMjIgIH0KICAgIH07CgogICAgdmFyIHNvcnRCeUFnZUFzYyA9IChDb21wYXJpc29uPFBlcnNvbj4pKCh4LCB5KSA9PgogICAgewogICAgICBpbnQgcmVzdWx0ID0geC5BZ2UuQ29tcGFyZVRvKHkuQWdlKTsKCiAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9KTsKCiAgICB2YXIgc29ydEJ5QWdlRGVzYyA9IChDb21wYXJpc29uPFBlcnNvbj4pKCh4LCB5KSA9PgogICAgewogICAgICBpbnQgcmVzdWx0ID0geS5BZ2UuQ29tcGFyZVRvKHguQWdlKTsKCiAgICAgIHJldHVybiByZXN1bHQ7CiAgICB9KTsKCiAgICB2YXIgc29ydCA9IG5ldyBGdW5jPHN0cmluZywgQ29tcGFyaXNvbjxQZXJzb24+PihmID0+CiAgICB7CiAgICAgIGlmIChmID09ICJBc2MiKSByZXR1cm4gc29ydEJ5QWdlQXNjOwogICAgICBlbHNlIGlmIChmID09ICJEZXNjIikgcmV0dXJuIHNvcnRCeUFnZURlc2M7CiAgICAgIHJldHVybiBudWxsOwogICAgfSk7CgogICAgcGVyc29uTGlzdC5Tb3J0KHNvcnQoIkRlc2MiKSk7CgogICAgZm9yZWFjaChQZXJzb24gcGVyc29uIGluIHBlcnNvbkxpc3QpCiAgICB7CiAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCQie3BlcnNvbi5OYW1lfSB7cGVyc29uLkFnZX0iKTsKICAgIH0KICB9CgogIHB1YmxpYyBjbGFzcyBQZXJzb24KICB7CiAgICBwdWJsaWMgc3RyaW5nIE5hbWUgeyBnZXQ7IHNldDsgfQogICAgcHVibGljIGludCBBZ2UgeyBnZXQ7IHNldDsgfQogIH0KfQ=="},"asBuffer":null},"loaded":true}}
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; }
  }
}