Today I saw a blog about SortedList calss. This class can sort your datas alphabetical. But you must know it sort acccording to the Key but not the value. if you change the key sequence, the output will change according to new key sort.
OK, let's go!
using System.Collections;//
namespace SortListClass
{
class Program
{
static void Main(string[] args)
{
SortedList mySortList = new SortedList();
mySortList["b"] = "张三";
mySortList["A"] = "李四";
mySortList["D"] = "王五";
mySortList["c"] = "赵六";
foreach (DictionaryEntry Item in mySortList)
{
Console.WriteLine(Item.Key.ToString()+" : "+ Item.Value.ToString());
Console.WriteLine();
}
Console.WriteLine();
}
}
}
here is also a sample for website:
client code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>SortedList Class Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>This is a test for SortedList Class</h1>
<br />
<asp:DropDownList ID="ddlName" runat=server Width="100px"></asp:DropDownList>
<asp:Button ID="btnChoose" runat=server Text="Choose A Name"
onclick="btnChoose_Click" />
<br />
<br />
<br />
<br />
<asp:Label ID="lblMsg" runat=server></asp:Label>
</div>
</form>
</body>
</html>
code behind:
using System.Collections;//
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SortedList mySortList = new SortedList();
mySortList["张三"] = "我是张三";
mySortList["李四"] = "我是李四";
mySortList["王五"] = "我是王五";
mySortList["赵六"] = "我是赵六";
mySortList["阿福"] = "我是阿福";
if (!IsPostBack)
{
foreach (DictionaryEntry Item in mySortList)
{
ListItem newListItem = new ListItem();
newListItem.Text = Item.Key.ToString();
newListItem.Value = Item.Value.ToString();
ddlName.Items.Add(newListItem);
}
}
}
protected void btnChoose_Click(object sender, EventArgs e)
{
lblMsg.Text = ddlName.SelectedItem.Value.ToString();
}
}
Wednesday, October 22, 2008
Subscribe to:
Posts (Atom)

