.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

SortedList in C#

Posted By:Praveen       Posted Date: December 11, 2008    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  
 

SortedList class object can store key/value pair object and gives you the sorted result based on the key. Sorted List can capable to sort DateTime,Date, Numeric, String Values.That Means whatever will be in the key place will be sorted.

The best use of sortedList Class is used to bind DropDownList, RadioButtonList etc which takes key/value pair.

SortedList Class availabe inside System.Collection Namespace

Below Example is used to sort Based on nemeric value

//Create sortedlist class obect

SortedList sortedList = new SortedList();

//Add Some key/value to SortedList Class Objetcs

sortedList.Add(1, "ASP.NET");

sortedList.Add(3, "C#");

sortedList.Add(6, "VB.NET");

sortedList.Add(4, "AJAX");

The Result Will be

ASP.NET
C#
Ajax





VB.NET

Similarly below example is used to sort collection based on string key value.

//Create sortedlist class obect

SortedList sortedList = new SortedList();

//Add Some key/value to SortedList Class Objetcs

sortedList.Add("ASP.NET", 1);

sortedList.Add("C#", 2);

sortedList.Add("VB.NET", 6);

sortedList.Add("AJAX", 3);

And the result will be

AJAX
ASP.NET
C#
VB.NET

Happy Coding!!!


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend