-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortInput.cs
More file actions
39 lines (31 loc) · 1.07 KB
/
SortInput.cs
File metadata and controls
39 lines (31 loc) · 1.07 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
public class SortInput
{
public List<KeyValuePair<string, int>> Sort(Dictionary<string, int> InputOccurences)
{
var unsortedList = new List<KeyValuePair<string, int>>(InputOccurences);
var sortedList = SortRoutine(unsortedList);
return sortedList;
}
/* Borrowed from Interview Point
// Compares every word in the list by checking if it's neighbour is higher on the list alphabetically
// If higher then the order of the words is moved untill it becomes completely alphabetical
*/
private List<KeyValuePair<string, int>> SortRoutine(List<KeyValuePair<string, int>> list)
{
var unsortedList = list;
int n = list.Count;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (string.Compare(list[j].Key, list[j + 1].Key) > 0)
{
var temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
return unsortedList;
}
}