C # Dictionary dictionary sort (sequential, reverse)

Source: Internet
Author: User

This is for. NET version is too low to sort the way, not how to use, record a bit;

First, create a dictionary Dictionary object

If the Dictionary is a website page traffic, key is the page name, the value of the page is the number of visits, because of the page's access to the statistics, so can not use int as key, only with the page name, create Dictionary object and add data code as follows:

dictionary<string,int> dic =Newdictionary<string,int>(); Dic. ADD ("index.html", -); Dic. ADD ("product.html", -); Dic. ADD ("aboutus.html",4); Dic. ADD ("online.aspx", A); Dic. ADD ("news.aspx", -);
Ii.. NET 2.0 version dictionary sorting
list<keyvaluepair<stringintnew list<keyvaluepair<stringint >>(DIC);

Flashback arrangement: Simply swap the variable s2 and S1 for an example: return S1.Value.CompareTo (S2. Value);
Sorting is now in order

lst. Sort (delegate(keyvaluepair<stringint> S1, keyvaluepair<string int> s2)  {  return  s2.Value.CompareTo (S1.      Value);      }); Dic. Clear ();
Iii.. NET 3.5 or later dictionary sorting (that is, LINQ dictionary sorting)

Sort by using LINQ

var  from inch  by Select objdic;
output to use this output: foreach (keyvaluepair<stringintin ": " " <br/>");}

C # Dictionary dictionary sort (sequential, reverse)

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.