C # sort and convert a dictionary

Source: Internet
Author: User

C #. after a version of net 3.5 or later is introduced to LINQ, Dictionary dictionary sorting becomes very simple and can be done with a query statement similar to SQL database; however ,. NET 2.0 sorting requires a little trouble. For ease of use, we will summarize. net 3.5 and 2.0 sorting methods.

 

1. Create a dictionary object

Assume that the traffic of a website page is stored in dictionary, the key is the webpage name, and the value corresponds to the number of visits to the webpage. As the webpage access is continuously counted, therefore, you cannot use int as the key, but only the webpage name. The code for creating a dictionary object and adding data is as follows:

Dictionary <string, int> DIC = new dictionary <string, int> ();
Dic. Add ("index.html", 50 );
Dic. Add ("product.html", 13 );
Dic. Add ("aboutus.html", 4 );
Dic. Add ("online. aspx", 22 );
Dic. Add ("news. aspx", 18 );

 

Ii... Net 3.5 and later versions of dictionary sorting (that is, LINQ dictionary sorting)

1. Sort dictionary by value

Private void dictonarysort (Dictionary <string, int> DIC)
{
VaR dicsort = from objdic in DIC orderby objdic. Value descending select objdic;
Foreach (keyvaluepair <string, int> kVp in dicsort)
Response. Write (kVp. Key + ":" + kVp. Value + "<br/> ");
}

Sorting result:

Index.html: 50
Online. aspx: 22
News. aspx: 18
Product.html: 13
Aboutus.html: 4

The above code is in descending order. to sort the code in ascending order, you only need to remove the descending on the right of the variable dicsort.

 

2. C # Sort dictionary keys

To sort by key, you only need to change objdic. value on the right of the variable dicsort to objdic. Key.

 

 

Iii... NET 2.0 dictionary sorting

1. Sort dictionary values in descending order)

Private void dictionarysort (Dictionary <string, int> DIC)
{
If (DIC. Count> 0)
{
List <keyvaluepair <string, int> lst = new list <keyvaluepair <string, int> (DIC );
Lst. Sort (delegate (keyvaluepair <string, int> S1, keyvaluepair <string, int> S2)
{
Return s2.value. compareto (s1.value );
});
Dic. Clear ();

Foreach (keyvaluepair <string, int> kVp in lst)
Response. Write (kVp. Key + ":" + kVp. Value + "<br/> ");
}
}

Sorting result:

Index.html: 50
Online. aspx: 22
News. aspx: 18
Product.html: 13
Aboutus.html: 4

Ordered sorting: you only need to change the variable return s2.value. compareto (s1.value) to return s1.value. compareto (s2.value.

 

2. C # Sort the dictionary keys in descending order)

If you want to sort by key, you only need to put return s2.value in reverse order. compareto (s1.value); changed to return s2.key. compareto (s1.key); return s2.key in sequence. compareto (s1.key); changed to return s1.key. compareto (s2.key.

C # sort and convert a dictionary

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.