C # Key-value pairs sort

Source: Internet
Author: User

Static void Main(String[]Args) { SortedListSL= New SortedList();Sl.Add("001", "Zara Ali");Sl.Add("002", "Abida Rehman");Sl.Add("003", "Joe Holzner.");Sl.Add("004", "Mausam Benazir Nur");Sl.Add("005", "M. Amlan");Sl.Add("006", "M. Arif");Sl.Add("007", "Ritesh Saikia"); If (Sl.Containsvalue("Nuha Ali")) { Console.WriteLine("This student name was already in the list"); } Else {Sl.Add("008", "Nuha Ali"); } Gets the collection of keys ICollectionKey=Slkeys;foreach  (string K in Key)  { Span class= "Typ" >console. Writeline (k +  ":"  + Sl[k);  } } 
================================= ======================================
C # Dictionary dictionary sort (sequential, reverse)

After the introduction of LINQ in C #. NET 3.5, the dictionary dictionary sort becomes quite simple, with a similar SQL database query statement, but the. NET 2.0 sort is a little cumbersome, and for ease of use, the sorting methods for. NET 3.5 and 2.0 are summarized. 。

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");

Ii.. NET 3.5 or later dictionary sorting (that is, LINQ dictionary sorting)

1. Sort by Value dictionary

  private void Dictonarysort (dictionary<string, int> dic)
{
    var dicsort = from objdic in dic by objdic.value Descending select Objdic;
    foreach (keyvaluepair<string, int> kvp in dicsort)
Response.Write (KVP. Key + ":" + kvp. Value + "<br/>");
}

Sort Result:

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

The above code is in descending order (in reverse order), if you want to sort in ascending (sequential), you just need to dicsort the variable to the right of the descending to remove.

2. C # Dictionary key sort

If you want to sort by Key, just change the objdic.value on the right side of the variable dicsort to Objdic.key.

iii.. NET 2.0 version dictionary sorting

1. Dictionary by value (reverse 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 s 2.value.compareto (S1. Value);
});
DiC. Clear ();

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

Sort Result:

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

Sequential arrangement: Only the variable return s2.Value.CompareTo (S1) is required. Value); Change to return S1.Value.CompareTo (S2. Value); Can.

2. C # Dictionary key sort (reverse, sequential)

If you want to sort by Key, reverse only s2.Value.CompareTo return (S1. Value); Change to return S2.Key.CompareTo (S1. Key); order Simply put return S2.Key.CompareTo (S1. Key); Change to return S1.Key.CompareTo (S2. Key); Can.

            

C # Key-value pairs sort

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.