Skip to content

Instantly share code, notes, and snippets.

@DreamerDeLy
Last active April 11, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DreamerDeLy/93dcb8998be48252edb949e586e01ac9 to your computer and use it in GitHub Desktop.
Save DreamerDeLy/93dcb8998be48252edb949e586e01ac9 to your computer and use it in GitHub Desktop.
Task 12 [C#]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Task12
{
class Program
{
static void Main(string[] args)
{
string path_before = @"before.txt";
string path_after_order = @"after_order.txt";
string path_after_sort = @"after_sort.txt";
if (File.Exists(path_before))
{
// Зчитуємо файл і додаємо строки в список
List<string> list_original =
new List<string>(File.ReadAllLines(path_before));
Console.WriteLine("Original list:");
list_original.ForEach(Console.WriteLine);
// Створюмо список відсортований першим способом
List<string> list_order = list_original;
list_order = list_order.OrderBy(num => num).ToList();
Console.WriteLine("\nNew sorted by \".OrderBy()\":");
list_order.ForEach(Console.WriteLine);
// Створюємо список відсортований другим способом
List<string> list_sort = list_original;
list_sort.Sort(Compare);
Console.WriteLine("\nNew sorted by \".Sort()\":");
list_sort.ForEach(Console.WriteLine);
// Зберігаємо файли
File.WriteAllLines(path_after_order, list_order);
File.WriteAllLines(path_after_sort, list_sort);
}
else
{
Console.WriteLine("File don't exist");
}
}
private static int Compare(string a, string b)
{
int x = int.Parse(a);
int y = int.Parse(b);
if (x > y)
{
return 1;
}
else if (x < y)
{
return -1;
}
else
{
return 0;
}
}
}
}
C:\AP\Task12>dotnet run
Original list:
8
5
6
2
1
0
12
5
7
9
New sorted by ".OrderBy()":
0
1
12
2
5
5
6
7
8
9
New sorted by ".Sort()":
0
1
2
5
5
6
7
8
9
12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment