C # about dictionary dictionary sorted by time

Source: Internet
Author: User

In the above mentioned SortedList sort is the key sort, does not conform to the project requirement problem, then uses the dictionary dictionary  to the value for the time in ascending order, the problem solves.
The middle involves the use of LINQ. There are other writing, but found the bottom of the wording is most intuitive and easy to understand.
var dicsort = from Objdic in dic by objdic.value descending select Objdic; This is the simplest and easiest to understand.
Dicsort the target dictionary, the Objdic object should be a mapped object that does not need to be defined, DIC is your own dictionary of definitions, order by rules, for each object in the dictionary or the value to be stored, the value of values in ascending sequence, select this object, the back Select Specifies that the format for this LINQ requirement is selected and is not understood in detail.
Take a look at the wording below.
For a dictionary<tkey, tvalue> The value sort can be used with LINQ:    Dictionary<stringNew dictionary<stringstring>();     = ( from in mydictionary                                        the  entry. Value Ascending                                       Select entry). ToDictionary (pair = = pair. Key, pair = = pair. Value);  

There is a todictionnary, this should be the source data is not a dictionary form, converted to a dictionary, and the current project inside the use of dictionary processing, so do not need to convert.

Defining dictionaries
Static New Dictionary<string, datetime> ();

Time variable

DateTime time = DateTime.Parse (strs[1]);
Add to

N_readlist. ADD (result, time);

Sort Ascending Ascending

var dicsort = from Objdic in n_readlist objdic.value ascending select Objdic;

Print

foreach (var item in Dicsort)
{
Debug.WriteLine (item. key+ "" + Item. Value);
}


var dicsort = from Objdic in n_readlist objdic.value ascending select Objdic;
Reference

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 = 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 2.0 version dictionary sorting
list<keyvaluepair<string, int>> lst = new list<keyvaluepair<string, int>> (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<string, int> 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 dicsort = from Objdic in dic by objdic.value descending select Objdic;
The output is to be used with this output: foreach (keyvaluepair<string, int> kvp in Dicsort) {Response.Write (KVP). Key + ":" + kvp. Value + "<br/>");}

C # about dictionary dictionary sorted by time

Related Article

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.