Dictionary sort descending
dictionary-sort-descending
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//initialize a dictionary with values.
Dictionary<int, string> birds = new Dictionary<int, string>() {
{1,"Arctic Tern"},
{2,"Roseate Tern"},
{3,"Black Tern"},
{4,"South Polar Skua"},
{5,"Herring Gull"}
};
Label1.Text = "birds dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
//this line sort dictionary by values.
birds = birds.OrderByDescending(x=>x.Value).ToDictionary(x=>x.Key,x=>x.Value);
Label1.Text += "<br /><br />descending sorted dictionary by values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary sort descending</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary sort descending
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="dictionary sort descending"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>
- How to sort a Dictionary by key
- How to sort a Dictionary by value
- How to sort a Dictionary by value in descending order
- How to use Dictionary TryGetValue
- How to make Dictionary TryGetValue case insensitive
- How to remove items from a Dictionary by condition
- How to remove an item from Dictionary by value
- How to get first key value from Dictionary
- How to merge two Dictionaries
- How to union two Dictionaries