Dictionary sort by value
dictionary-sort-by-value.aspx
<%@ 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> plants = new Dictionary<int, string>() {
{1,"Orange Milkweed"},
{2,"Horsetail Milkweed"},
{3,"American Nightshade"},
{4,"Yellow Milkweed"},
{5,"Swamp Milkweed"}
};
Label1.Text = "plants dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in plants)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
Label1.Text += "<br /><br />plants dictionary keys and values sorted by values..........";
foreach (KeyValuePair<int, string> pair in plants.OrderBy(pair=>pair.Value))
{
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 by value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary sort by value
</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 by value"
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 in descending order
- How to sort a Dictionary in descending order
- How to use Dictionary TryGetValue
- How to make Dictionary TryGetValue case insensitive
- How to remove items from a Dictionary while iterating
- How to remove items from a Dictionary in loop
- How to remove range of items from a Dictionary
- How to get a range of items from a Dictionary
- How to remove items from a Dictionary by condition