Free Information Technology Magazines and eBooks

Sunday, March 07, 2010

Automatic Sorting Of List Using SortedSet In.NET 4.0

SortedSet Class is one of the newest class library in .NET Framework 4.0. It represents a collection of objects that is maintained in sorted order. To demonstrate how it use, here is a sample code that initially inserted some random strings and then later on these strings will be displayed in sorted sequence.


SortedSet Sample Code In C#:


var sSet = new SortedSet { };

//Add unsorted strings
sSet.Add("Fryan");
sSet.Add("Sisa");
sSet.Add("Auric");
sSet.Add("Juan");


//display sorted strings
foreach (string s in sSet)
{
Console.WriteLine(s);
}
Console.ReadKey();


In VB.NET:


Dim sSet As New SortedSet(Of String)

'Add unsorted strings
sSet.Add("Fryan")
sSet.Add("Auric")
sSet.Add("Sisa")
sSet.Add("Juan")

'display sorted strings
For Each s As String In sSet
Console.WriteLine(s)
Next

Console.ReadKey()


To stay up-to-date on .NET Framework 4.0 Coding Tips, subscribe now.

2 comments:

Anonymous said...

good information. Below is a link that provides good details about SortedSet

http://www.a2zmenu.com/Silverlight/Calling-JavaScript-Method-from-Silverlight.aspx

ASP.NET Solution said...

SortedSet is new collection introduced in C# 4.0.

Below link provides good details about SortedSet

http://www.a2zmenu.com/CSharp/SortedSet-Collection-Class.aspx