Sorting Dictionaries

Why do dictionaries sort

Sometimes we use dictionaries for easy storage , But we don't use it very often , Sometimes we have to sort things out because of the need of the project , For example, one of the problems I encountered recently is , So I learned some sorting methods to share with you .

Method 1 :
This is a relatively simple method in my opinion .
For example, I define a more complex dictionary , To sort this dictionary into school , According to his value How many strings does the value store .
// Dictionary to sort private Dictionary<string, List<string>> LineBlocks = new
Dictionary<string, List<Block>>(); // It is assumed that there is data in the dictionary
// Here, turn the dictionary into a linked list , Because dictionaries can't sort directly . List<KeyValuePair<string, List<string>>>
Linelist = new List<KeyValuePair<string, List<string>>>(LineBlocks);
// Using linked list Sort Methods Linelist.Sort( delegate(KeyValuePair<string, List<string>>
s1, KeyValuePair<string, List<string>> s2) {
// This determines whether your sort order is large or small to large , My order here is from small to large . If you want to change the sort order , Direct exchange s1 and s2 That's it . return
s1.Value.Count .CompareTo(s2.Value.Count); }); LineBlocks.Clear(); foreach
(KeyValuePair< string,List<string>> ss in Linelist) { LineBlocks[ss.Key] =
ss.Value; }
Here the dictionary has been reordered .

Here we add a method to reverse the elements in the linked list : LineBlocks.Reverse();

Method 2 :
This is a big problem for me , Not recommended .
// Dictionary to sort private Dictionary<string, List<string>> LineString= new
Dictionary<string, List<Block>>(); // It is assumed that there is data in the dictionary // Declare a class , The fields in the dictionary are stored separately Key and Value
public class MyClass { public string str; public List<string> liststr; }
List<MyClass> myclass = new List<MyClass>(); foreach (string str in
LineString.Keys) { MyClass my = new MyClass(); my.str= str ; my.liststr=
LineString[str]; myclass.Add(my); } LineString.Clear(); // Here we use bubble sort to sort MyClass
temp; for (int i = 0; i < myclass.Count; i++) { for (int j = 0; j <
myclass.Count-1-i; j++) { if
(myclass[j].liststr.Count>myclass[j+1].liststr.Count) { temp = myclass[j + 1];
myclass[j + 1] = myclass[j]; myclass[j] = temp; } } } // Here we add the dictionary again foreach
(MyClass my in myclass) { LineString[my.str]=my.liststr; }
The dictionary is reordered . According to value The value of the , from small to large .

©2019-2020 Toolsou All rights reserved,
A summary of the computer retest interview questions var a = 0, b = 0; function A(a) {A = function (b)... An interview question about scope 【 Data structure and algorithm 1】 Sparse array 2020—— Net tripod cup part writeupPTA-MOOC《Python Programming Zhejiang University 》 Puzzle A Chapter 2 programming problems python closure uniapp Dynamic routing parameter transfer ( Delivery object ) How to use it Python Batch import Excel Combined use Pandas integration Lambda Expression and functional interface 【Java Details of knowledge points 3】 Serializable and Deserialize