-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (72 loc) · 2.58 KB
/
Copy pathProgram.cs
File metadata and controls
85 lines (72 loc) · 2.58 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// See https://aka.ms/new-console-template for more information
using GRHWConsole;
if (args.Length == 0)
{
// Normally this and other messages would be stored elsewhere
// to make them easy to modify without having to recompile
Console.WriteLine("You must provide a file path parameter");
}
else
{
if (File.Exists(args[0]))
{
Console.WriteLine($"Reading data from {args[0]}");
try
{
string[] lines = await File.ReadAllLinesAsync(args[0]);
var data =
(from line in
from line in lines
select line.Split(',')
.ToArray<string>()
select new SomeData()
{
LastName = line[(int)ArrayElement.LastName],
FirstName = line[(int)ArrayElement.FirstName],
Email = line[(int)ArrayElement.Email],
FavoriteColor = line[(int)ArrayElement.FavoriteColor],
DateOfBirth = DateTime.Parse(line[(int)ArrayElement.DoB])
})
.ToList<SomeData>();
var check = data;
if (args.Length > 1)
{
Console.WriteLine($"Sort by {args[1]}");
string sortBy = args[1];
switch(sortBy.ToLower())
{
case "color":
data = data.OrderBy(d => d.FavoriteColor)
.ThenBy(d => d.LastName)
.ToList<SomeData>();
break;
case "birthdate":
data = data.OrderBy(d => d.DateOfBirth)
.ToList<SomeData>();
break;
case "name":
data = data.OrderByDescending(d => d.LastName)
.ToList<SomeData>();
break;
default:
Console.WriteLine("Invalid sort parameter provided");
break;
}
}
foreach (var line in data)
{
Console.WriteLine(line);
}
Console.WriteLine("Done");
}
// for sake of brevity, I am only catching generic exception
catch (Exception)
{
throw;
}
}
else
{
Console.WriteLine("The file specified does not exist");
}
}