Digital Colony!

SortedList Example for ASP.NET in C#

The SortedList object uses a key/value combination to sort a list. The SortedList can be databound to the following ASP.NET controls:
  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:ListBox
Our examples will bind a SortedList to an asp:ListBox.
<asp:ListBox ID="lsbPresidents" runat="server" />

Numeric Sort

SortedList s = new SortedList();
s.Add(16, "Lincoln");
s.Add(42, "Clinton");
s.Add(40, "Reagan");
s.Add(1, "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Washington
Lincoln
Reagan
Clinton      

Alphabetic

SortedList s = new SortedList();
s.Add("L", "Lincoln");
s.Add("C", "Clinton");
s.Add("R", "Reagan");
s.Add("W", "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Clinton   
Lincoln  
Reagan 
Washington

Date Sort

SortedList s = new SortedList();
s.Add(Convert.ToDateTime("2/12/1809"), "Lincoln");
s.Add(Convert.ToDateTime("8/19/1946"), "Clinton");
s.Add(Convert.ToDateTime("2/6/1911"), "Reagan");
s.Add(Convert.ToDateTime("2/22/1732"), "Washington");

lsbPresidents.DataSource = s;
lsbPresidents.DataValueField = "key";
lsbPresidents.DataTextField = "value";
lsbPresidents.DataBind();
Results:
Washington
Lincoln
Reagan
Clinton      

Labels:

AddThis Social Bookmark Button

4 Comments:

Anonymous Anonymous said...

Hi,
Error thrown when tried to search an item from Numeric dropdown.
i.e
dim flag as boolean
flag= s.ContainsKey(1)

Please suggest solution.

Thanks

12/01/2007 4:58 AM

Blogger MAS said...

You didn't specify the error thrown and dim indicates you are using VB.NET.

I tested successfully a numeric search in C# with the following code.

Boolean flag;
flag = s.ContainsKey(1);
if (flag)
lblFlag.Text = "List has #1";

12/01/2007 5:31 AM

Blogger Hanife DoÄŸanay said...

simple and good example

8/19/2008 1:04 AM

OpenID ycuentocorto said...

The example is great , the Date example is a good "trick".

I have a problem with my Cpmparer class (implementing IComparer interface), I need sort the list

A1
A20
A34
A69
A7

Result

A1
A7
A20
A34
A69

My implementation appear do not work when I create new SortedList< string >(new myComparerClass).

Any suggestion?


public int Compare(string x, string y)
{
string xString = "";
string xNumber = "";
int xNumberVal = 0;

string yString = "";
string yNumber = "";
int yNumberVal =0;

for(int i = 0; i < x.Length; i++) {
if (Char.IsLetter(x[i])) {
xString += x[i];
} else {
xNumber = x.Substring(i,x.Length -i);
xNumberVal = Convert.ToInt32(xNumber);
break;
}
}

for(int i = 0; i < y.Length; i++) {
if (Char.IsLetter(y[i])) {
yString += y[i];
} else {
yNumber = y.Substring(i,y.Length -i);
yNumberVal = Convert.ToInt32(yNumber);
break;
}
}
int n = xNumberVal.CompareTo(yNumberVal);
int s = xString.CompareTo(yString);

if (xString == yString) {
return xNumberVal.CompareTo(yNumberVal);
} else {
return xString.CompareTo(yString);
}

}
}

10/08/2008 3:13 PM

 

Post a Comment

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.