| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5821cf2 commit 0170774
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,24 @@ namespace RoyalLibrary.Tests | |||
| 8 | 8 | { | |
| 9 | 9 | public class SortingExtensionTests | |
| 10 | 10 | { | |
| 11 | + // Arrange | ||
| 12 | + private readonly List<string> _unsortedInputList = new List<string>() | ||
| 13 | + { | ||
| 14 | + "Ai Bi Bu", | ||
| 15 | + "Ai Bi Az", | ||
| 16 | + "Na Za", | ||
| 17 | + "Xa Ma Co", | ||
| 18 | + "AA bb" | ||
| 19 | + }; | ||
| 20 | + private readonly List<string> _expectedOutputList = new List<string>() | ||
| 21 | + { | ||
| 22 | + "Ai Bi Az", | ||
| 23 | + "AA bb", | ||
| 24 | + "Ai Bi Bu", | ||
| 25 | + "Xa Ma Co", | ||
| 26 | + "Na Za", | ||
| 27 | + }; | ||
| 28 | + | ||
| 11 | 29 | [Fact] | |
| 12 | 30 | public async void SortByLastNameAsync_ThrowArgumentNullException_WhenSourceIsNull() | |
| 13 | 31 | { | |
@@ -36,29 +54,25 @@ public async void SortByLastNameAsync_ReturnsSourceCOllections_WhenSourceSizeIsZ | |||
| 36 | 54 | public async void SortByLastNameAsync_ReturnsTheRightSortedList_WhenCorrectSourceIsProvided() | |
| 37 | 55 | { | |
| 38 | 56 | // Arrange | |
| 39 | - var unSortedlist = new List<string>() | ||
| 40 | - { | ||
| 41 | - "Ai Bi Bu", | ||
| 42 | - "Ai Bi Az", | ||
| 43 | - "Na Za", | ||
| 44 | - "Xa Ma Co", | ||
| 45 | - "AA bb" | ||
| 46 | - }; | ||
| 47 | - var expected = new List<string>() | ||
| 48 | - { | ||
| 49 | - "Ai Bi Az", | ||
| 50 | - "AA bb", | ||
| 51 | - "Ai Bi Bu", | ||
| 52 | - "Xa Ma Co", | ||
| 53 | - "Na Za", | ||
| 57 | + // Act | ||
| 58 | + var actual = await _unsortedInputList.SortByLastNameAsync(); | ||
| 59 | + | ||
| 60 | + // Assert | ||
| 61 | + Assert.True(_expectedOutputList[1] == actual.ElementAt(1)); | ||
| 62 | + } | ||
| 54 | 63 | ||
| 55 | - }; | ||
| 64 | + [Fact] | ||
| 65 | + public async void SortByLastNameAsyncWithAction_ReturnsTheRightSortedList_WhenCorrectSourceIsProvided() | ||
| 66 | + { | ||
| 67 | + // Arrange | ||
| 68 | + var count = 0; | ||
| 56 | 69 | ||
| 57 | 70 | // Act | |
| 58 | - var actual = await unSortedlist.SortByLastNameAsync(); | ||
| 71 | + var actual = await _unsortedInputList.SortByLastNameAsync(_ => count++); | ||
| 59 | 72 | ||
| 60 | 73 | // Assert | |
| 61 | - Assert.True(expected[1] == actual.ElementAt(1)); | ||
| 74 | + Assert.True(_expectedOutputList[1] == actual.ElementAt(1)); | ||
| 75 | + Assert.Equal(5, count); | ||
| 62 | 76 | } | |
| 63 | 77 | } | |
| 64 | 78 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,6 @@ | |||
| 5 | 5 | ||
| 6 | 6 | namespace ByteDecoder.RoyalLibrary | |
| 7 | 7 | { | |
| 8 | - | ||
| 9 | 8 | /// <summary> | |
| 10 | 9 | /// Advanced sorting for collections | |
| 11 | 10 | /// </summary> | |
@@ -16,131 +15,59 @@ public static class SortingExtensions | |||
| 16 | 15 | /// </summary> | |
| 17 | 16 | /// <param name="source"></param> | |
| 18 | 17 | /// <returns>An Enumerable sorted collection by LastName</returns> | |
| 19 | - public async static Task<IEnumerable<string>> SortByLastNameAsync(this IEnumerable<string> source) | ||
| 18 | + public async static Task<ICollection<string>> SortByLastNameAsync(this IEnumerable<string> source) | ||
| 20 | 19 | { | |
| 21 | 20 | if (source == null) | |
| 22 | 21 | throw new ArgumentNullException(nameof(source)); | |
| 23 | 22 | ||
| 24 | 23 | int rowsCount = source.Count(); | |
| 25 | 24 | ||
| 26 | - if (rowsCount == 0) return source; | ||
| 25 | + if (rowsCount == 0) return (ICollection<string>)source; | ||
| 27 | 26 | ||
| 28 | 27 | byte lastspaceIndex = 0; | |
| 29 | 28 | string firstName, lastName = null; | |
| 30 | - var tempDic = new MultiKeysValue() { Capacity = rowsCount }; | ||
| 31 | - var tempList = new List<string>(rowsCount); | ||
| 32 | - var tempResult = new List<string>(rowsCount); | ||
| 29 | + var orderedList = new SortedList<string, string>() { Capacity = rowsCount }; | ||
| 33 | 30 | ||
| 34 | 31 | await Task.Run(() => | |
| 35 | 32 | { | |
| 36 | 33 | foreach (var item in source) | |
| 37 | 34 | { | |
| 38 | - | ||
| 39 | 35 | var spaces = item.Count(char.IsWhiteSpace); | |
| 40 | 36 | lastspaceIndex = (byte)item.IndexOfNth(' ', spaces - 1); | |
| 41 | 37 | ||
| 42 | - //get the whole firstname(s) , then the lastName, add them to tempDic which will be used | ||
| 43 | - //to get firstname(s) by lastName | ||
| 44 | - //lastname will be added to the temp list and sorted everytime | ||
| 45 | 38 | firstName = item.Substring(0, lastspaceIndex); | |
| 46 | 39 | var lastNameLength = item.Length - firstName.Length; | |
| 47 | 40 | lastName = item.Substring(lastspaceIndex, lastNameLength); | |
| 48 | 41 | ||
| 49 | - tempDic.Add(firstName, lastName); | ||
| 50 | - tempList.Add(lastName); | ||
| 51 | - } | ||
| 52 | - tempList.Sort(); | ||
| 53 | - | ||
| 54 | - }).ContinueWith(async (filtering) => | ||
| 55 | - { | ||
| 56 | - foreach (var LastName in tempList) | ||
| 57 | - { | ||
| 58 | - //get the firstName(s) from tempDic by lastName | ||
| 59 | - | ||
| 60 | - var dicfirstName = tempDic.AsParallel().First(x => x.Value == LastName).Key; | ||
| 61 | - await Task.Delay(0);//fake await to run everything smoothly | ||
| 62 | - //combine names again with sorted | ||
| 63 | - tempResult.Add(dicfirstName + LastName); | ||
| 64 | - | ||
| 42 | + orderedList.Add(lastName, firstName); | ||
| 65 | 43 | } | |
| 66 | 44 | }); | |
| 67 | - tempDic = null; | ||
| 68 | - tempList = null; | ||
| 69 | - return tempResult; | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | 45 | ||
| 46 | + return orderedList.AsParallel() | ||
| 47 | + .AsOrdered() | ||
| 48 | + .Select((fullName) => fullName.Value + fullName.Key).ToList(); | ||
| 75 | 49 | } | |
| 50 | + | ||
| 76 | 51 | /// <summary> | |
| 77 | 52 | /// returns a new sorted ICollection | |
| 78 | 53 | /// </summary> | |
| 79 | 54 | /// <param name="source"></param> | |
| 80 | 55 | /// <param name="action"></param> | |
| 81 | 56 | /// <returns>An Enumerable sorted collection by LastName</returns> | |
| 82 | - public async static Task<ICollection<string>> RoyalSortLastAsync(this IEnumerable<string> source, Action<string> action) | ||
| 57 | + public async static Task<IEnumerable<string>> SortByLastNameAsync(this IEnumerable<string> source, Action<string> action) | ||
| 83 | 58 | { | |
| 84 | - int rowsCount = source.Count(); | ||
| 85 | - byte lastspaceIndex = 0; | ||
| 86 | - string firstName, lastName = null; | ||
| 87 | - var tempDic = new MultiKeysValue() { Capacity = rowsCount }; | ||
| 88 | - var tempList = new List<string>(rowsCount); | ||
| 89 | - var tempResult = new List<string>(rowsCount); | ||
| 90 | - | ||
| 91 | - await Task.Run(() => | ||
| 92 | - { | ||
| 93 | - foreach (var item in source) | ||
| 94 | - { | ||
| 95 | - | ||
| 96 | - var spaces = item.Count(char.IsWhiteSpace); | ||
| 97 | - lastspaceIndex = (byte)item.IndexOfNth(' ', spaces - 1); | ||
| 98 | - | ||
| 99 | - //get the whole firstname(s) , then the lastName, add them to tempDic which will be used | ||
| 100 | - //to get firstname(s) by lastName | ||
| 101 | - //lastname will be added to the temp list and sorted everytime | ||
| 102 | - firstName = item.Substring(0, lastspaceIndex); | ||
| 103 | - var lastNameLength = item.Length - firstName.Length; | ||
| 104 | - lastName = item.Substring(lastspaceIndex, lastNameLength); | ||
| 105 | - | ||
| 106 | - tempDic.Add(firstName, lastName); | ||
| 107 | - tempList.Add(lastName); | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 111 | - } | ||
| 112 | - tempList.Sort(); | ||
| 113 | - | ||
| 114 | - }).ContinueWith(async (filtering) => | ||
| 115 | - { | ||
| 116 | - foreach (var LastName in tempList) | ||
| 117 | - { | ||
| 118 | - //get the firstName from tempDic by lastName | ||
| 119 | - | ||
| 120 | - var dicfirstName = tempDic.AsParallel().First(x => x.Value == LastName).Key; | ||
| 121 | - await Task.Delay(0);//fake await to run everything smoothly | ||
| 122 | - //combine names again with sorted | ||
| 123 | - tempResult.Add(dicfirstName + LastName); | ||
| 124 | - | ||
| 125 | - } | ||
| 126 | - }); | ||
| 127 | - tempDic = null; | ||
| 128 | - tempList = null; | ||
| 129 | - | ||
| 130 | - foreach (var item in tempResult.AsParallel()) | ||
| 131 | - { | ||
| 132 | - action.Invoke(item); | ||
| 133 | - } | ||
| 134 | - | ||
| 135 | - return tempResult; | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 59 | + var sortedList = await source.SortByLastNameAsync(); | ||
| 60 | + sortedList.AsParallel().ForAll(action); | ||
| 61 | + return sortedList; | ||
| 141 | 62 | } | |
| 142 | 63 | ||
| 143 | - | ||
| 64 | + /// <summary> | ||
| 65 | + /// | ||
| 66 | + /// </summary> | ||
| 67 | + /// <param name="str"></param> | ||
| 68 | + /// <param name="value"></param> | ||
| 69 | + /// <param name="nth"></param> | ||
| 70 | + /// <returns></returns> | ||
| 144 | 71 | private static int IndexOfNth(this string str, char value, int nth) | |
| 145 | 72 | { | |
| 146 | 73 | if (nth < 0) | |
@@ -156,16 +83,21 @@ private static int IndexOfNth(this string str, char value, int nth) | |||
| 156 | 83 | return offset; | |
| 157 | 84 | } | |
| 158 | 85 | ||
| 159 | - | ||
| 160 | - } | ||
| 161 | - class MultiKeysValue : List<KeyValuePair<string, string>> | ||
| 162 | - { | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - public void Add(string key, string value) | ||
| 86 | + /// <summary> | ||
| 87 | + /// | ||
| 88 | + /// </summary> | ||
| 89 | + class MultiKeysValue : List<KeyValuePair<string, string>> | ||
| 166 | 90 | { | |
| 167 | - var element = new KeyValuePair<string, string>(key, value); | ||
| 168 | - this.Add(element); | ||
| 91 | + /// <summary> | ||
| 92 | + /// | ||
| 93 | + /// </summary> | ||
| 94 | + /// <param name="key"></param> | ||
| 95 | + /// <param name="value"></param> | ||
| 96 | + public void Add(string key, string value) | ||
| 97 | + { | ||
| 98 | + var element = new KeyValuePair<string, string>(key, value); | ||
| 99 | + this.Add(element); | ||
| 100 | + } | ||
| 169 | 101 | } | |
| 170 | 102 | } | |
| 171 | - } | ||
| 103 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments