-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnameOrdering.cs
205 lines (196 loc) · 4.57 KB
/
nameOrdering.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace OrderNames
{
// Delegates to be used by Main
delegate NamePair[] SortFunc(NamePair[] unsortedNames);
delegate NamePair[] ReadData(string filePath, char[] split);
delegate void WriteData(string filePath, NamePair[] names);
delegate string OutputPath(string filePath);
struct NamePair
{
public string firstName;
public string lastName;
public NamePair()
{
firstName = null;
lastName = null;
}
public NamePair(string first, string last)
{
firstName = first;
lastName = last;
}
}
class OrderNames
{
/*
* Read name pairs in from a file in the following
* format: "lastName, firstName"
*/
public static NamePair[] readFile(string filePath, char[] split)
{
List<NamePair> names = new List<NamePair>();
string[] lines;
try
{
lines = File.ReadAllLines(filePath);
foreach(string s in lines)
{
NamePair name = new NamePair();
string[] temp = s.Split(split);
// If a name does not have two parts,
// leave the field as null and handle
// formatting later.
if(temp.Length >= 1)
{
name.lastName = temp[0].Trim();
if(temp.Length >= 2)
{
name.firstName = temp[1].Trim();
}
}
names.Add(name);
}
}
catch(ArgumentException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(DirectoryNotFoundException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(IOException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(UnauthorizedAccessException)
{
Console.WriteLine("Could not open the file " + filePath + " due to authorisation errors");
Environment.Exit(1);
}
return names.ToArray();
}
/*
* Write the data out to a given file in the following format
* "lastName, firstname"
*/
public static void writeFile(string filePath, NamePair[] names)
{
StreamWriter output;
try
{
output = new StreamWriter(filePath);
foreach(NamePair name in names)
{
// If a given name doesn't have a field
// specified, skip it
if(name.lastName != null)
{
output.Write(name.lastName);
}
output.Write(", ");
if(name.firstName != null)
{
output.Write(name.firstName);
}
output.WriteLine();
}
output.Close();
}
catch(ArgumentException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(DirectoryNotFoundException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(IOException)
{
Console.WriteLine("Invalid file path");
Environment.Exit(1);
}
catch(UnauthorizedAccessException)
{
Console.WriteLine("Could not open the file " + filePath + " due to authorisation errors");
Environment.Exit(1);
}
}
/*
* Order a set of names by last name, then firstname lexicographically.
*/
public static NamePair[] orderNames(NamePair[] names)
{
// let the compiler handle the type of linq expressions. They can
// be fiendishly difficult to get right.
var sortedNames =
from name in names
orderby name.lastName ascending, name.firstName ascending
select name;
return sortedNames.ToArray();
}
/*
* Create a modified filepath for the output
*/
public static string createOutputFilePath(string s)
{
string temp;
int cutPoint = s.LastIndexOf('.'); // Find location to cut the file type off
if(cutPoint != -1)
{
temp = s.Substring(0, cutPoint) + "-sorted.txt";
}
else
{
temp = s + "-sorted.txt";
}
return temp;
}
public static int Main(string[] args)
{
if(args.Length == 0)
{
Console.WriteLine("You must provide the file of names to sort");
return 1;
}
else
{
SortFunc sorter = orderNames;
ReadData dataInput = readFile;
WriteData dataOutput = writeFile;
OutputPath outputFilePath = createOutputFilePath;
string filePath = args[0];
string outPath = outputFilePath(filePath);
char[] splitChars = { ',' };
NamePair[] names ;
names = dataInput(filePath, splitChars);
names = sorter(names);
dataOutput(outPath, names);
foreach(NamePair name in names)
{
if(name.lastName != null)
{
Console.Write(name.lastName);
}
Console.Write(", ");
if(name.firstName != null)
{
Console.Write(name.firstName);
}
Console.WriteLine();
}
Console.WriteLine("Finished: created " + outPath);
}
return 0;
}
}
}