public static void ShowDictionaryTemplateWithStringKey() { var dictionary = new Dictionary <double>(); CollectionChangeEventHandler showInfo = (sender, e) => { Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"{e.Action}: {e.Element}", dictionary); }; dictionary.InsertEvent += showInfo; dictionary.RemoveEvent += showInfo; dictionary.ClearEvent += showInfo; dictionary.Add("четыре", 4.00); dictionary.Add("сто", 100.0); dictionary.Add("27", 27.11); dictionary.Add(new KeyValuePair <string, double>("минус девять", -9.000)); dictionary.Remove("27"); dictionary.Add(new KeyValuePair <string, double>("nul", 0)); dictionary.SortByKey(); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"Sort key: ", dictionary); dictionary.Add(new KeyValuePair <string, double>("mnogo", 1234567.8765432)); dictionary.SortByValue(); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"Sort value: ", dictionary); dictionary.Remove("сто"); dictionary.Clear(); dictionary.Add("fhk", 2232.3); dictionary.Add("qqqqqqqqqqqqq", -3); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); Console.WriteLine(dictionary["qqqqqqqqqqqqq"]); Console.WriteLine(dictionary["fhk"]); }
public static void ShowDictionary() { var dictionary = new Dictionary(); CollectionChangeEventHandler showInfo = (sender, e) => { Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"{e.Action}: {e.Element}", dictionary); }; dictionary.InsertEvent += showInfo; dictionary.RemoveEvent += showInfo; dictionary.ClearEvent += showInfo; dictionary.Add("stack", new Stack()); dictionary.Add("list", new List()); dictionary.Add("bit array", new BitArray()); dictionary.Add(new KeyValuePair <string, object>("linked list", new LinkedList())); dictionary.Remove("list"); dictionary.Add(new KeyValuePair <string, object>("collection", new Collection())); dictionary.SortByKey(); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"Sort key: ", dictionary); dictionary.Add(new KeyValuePair <string, object>("matrix", new Matrix(3, 5))); dictionary.SortByValue(); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); ShowAction($"Sort value: ", dictionary); dictionary.Remove("collection"); dictionary.Clear(); dictionary.Add("dictionary", new Dictionary()); dictionary.Add("list", new List()); Console.WriteLine($"Count = {dictionary.Count}, IsSorted = {dictionary.IsSorted}"); Console.WriteLine(dictionary["dictionary"]); Console.WriteLine(dictionary["list"]); }
public void SortByValueTest() { IDictionary<string, int> Test = new Dictionary<string, int>(); Test.Add("Q", 4); Test.Add("Z", 2); Test.Add("C", 3); Test.Add("A", 1); Test = Test.SortByValue(); string Value = ""; foreach (string Key in Test.Keys) Value += Test[Key].ToString(); Assert.Equal("1234", Value); }
public void SortByValueTest() { IDictionary <string, int> Test = new Dictionary <string, int>(); Test.Add("Q", 4); Test.Add("Z", 2); Test.Add("C", 3); Test.Add("A", 1); Test = Test.SortByValue(); string Value = ""; foreach (string Key in Test.Keys) { Value += Test[Key].ToString(); } Assert.Equal("1234", Value); }
public void TestSortByValue() { // Arrange var notSortedDictionary = new Dictionary <int, string> { { 1, "a" }, { 3, "c" }, { 2, "b" } }; var sortedDictionary = new Dictionary <int, string> { { 1, "a" }, { 2, "b" }, { 3, "c" }, }; // Act var result = notSortedDictionary.SortByValue(); // Assert result.Should().Equal(sortedDictionary); result.Should().Not.Equal(notSortedDictionary); }
public void SortByValue() { Dictionary<Int32, Char> input1 = new Dictionary<int, char>(3) { { 1, 'h' }, { 2, 'a' }, { 3, 'x' } }; Dictionary<Int32, Char> expected1 = new Dictionary<int, char>(3) { { 2, 'a' }, { 1, 'h' }, { 3, 'x' } }; Dictionary<Int32, Char> expected2 = new Dictionary<int, char>(3) { { 3, 'x' }, { 1, 'h' }, { 2, 'a' } }; CollectionAssert.AreEqual(expected1, input1.SortByValue(true)); CollectionAssert.AreEqual(expected2, input1.SortByValue(false)); }
private static void WriteResults <T1, T2>(Dictionary <T1, T2> dictionary, int maxWords, string fileName, string savePath) { var sortedTotalWords = dictionary.SortByValue().Take(maxWords); IOUtilities.Write(JsonConvert.SerializeObject(sortedTotalWords, Formatting.Indented), savePath + fileName + ".json"); }