C # talk about dictionary<tkey,tvalue>,sorteddictionary<tkey,tvalue> sort

Source: Internet
Author: User

People who have used dictionary know that when the values in each add do not change their order, they need to be sorted to use SortedDictionary, but SortedDictionary is not ideal, and its default way is only to support positive ordering. , you have to rewrite the code on your own when you want to reverse order, and here's a simple example:

Private voidTestdictionarysort () {SortedDictionary<string,string> SD =Newsorteddictionary<string,string>(); Sd. ADD ("321","Fdsgsags"); Sd. ADD ("ACB","test test"); Sd. ADD ("1123","LSLGSGL"); Sd. ADD ("2bcd13","value"); foreach(keyvaluepair<string,string> IteminchSD) {Response.Write ("Key Name:"+ Item. Key +"Key values:"+item.            Value); }        }

The above code output effect:

Key Name: 1123 key value: LSLGSGL
Key Name: 2bcd13 Key value: Value
Key Name: 321 Key value: Fdsgsags
Key Name: ACB key value: Test test

OK, now let's look at the effect of the reverse order, see the following code:

Private voidTestdictionarysort () {SortedDictionary<string,string> SD =Newsorteddictionary<string,string>(); Sd. ADD ("321","Fdsgsags"); Sd. ADD ("ACB","test test"); Sd. ADD ("1123","LSLGSGL"); Sd. ADD ("2bcd13","value"); Response.Write ("<br/> Sequencing data: <br/>"); foreach(keyvaluepair<string,string> IteminchSD) {Response.Write ("Key Name:"+ Item. Key +"Key values:"+ Item. Value +"<br/>"); }            //Reseal to Dictionary (PS: Because we will not use the sort after sorting, so we use dictionary)dictionary<string,string> DC =Newdictionary<string,string>(); foreach(keyvaluepair<string,string> IteminchSD. Reverse ()) {DC. ADD (item. Key, item.            Value); } SD=NULL; //and then look at the results of the output:Response.Write ("<br/> Reverse order data: <br/>"); foreach(keyvaluepair<string,string> IteminchDC) {Response.Write ("Key Name:"+ Item. Key +"Key values:"+ Item. Value +"<br/>"); }        }

The above code output effect:

To sort data in a positive order:
Key Name: 1123 key value: LSLGSGL
Key Name: 2bcd13 Key value: Value
Key Name: 321 Key value: Fdsgsags
Key Name: ACB key value: Test test

Order data in reverse order:
Key Name: ACB key value: Test test
Key Name: 321 Key value: Fdsgsags
Key Name: 2bcd13 Key value: Value
Key Name: 1123 key value: LSLGSGL

C # talk about dictionary<tkey,tvalue>,sorteddictionary<tkey,tvalue> sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.