C# – Sort list of filename according to its extension

Sorting is a common task which I should complete during my development. Sometimes it is very simple, sometimes it is very complicated according to the object which I want to sort. In this small post, I would like to make a simple example of sorting a list of file name to its extension. I will use 2 methods to sort this list: one with class implementing IComparer and one with LINQ.
The list of file names is initialized through hard-code as following

List<string> lstFile = new List<string>()
            {"1.png","180.PDF","Arial.ttf","BESTÄTIGUNGdoc.doc", "configuration.php",
             "config.php", "crossdomain.xml", "Daten.mdb", "ebook.chm", "export.swf" };

You can see that the list contains 10 items in random order. To sort them according to its extension I use 2 methods below

1. Implement IComparer<>
Using this method, I need to create a class which implements interface IComparer and its methods.

class ExtensionSorter : IComparer<string>
	#region IComparer<string> Members

	public int Compare(string x, string y)
		string strExt1 = Path.GetExtension(x);
		string strExt2 = Path.GetExtension(y);

		if (strExt1.Equals(strExt2))
			return x.CompareTo(y);
			return strExt1.CompareTo(strExt2);


As you can see because I am going to sort which string-object I can reuse the CompareTo function of string-class to compare. Then pass an instance of this class to Sort() method to sort our list

lstFile.Sort(new ExtensionSorter());
foreach (string strTemp in lstFile)

The output looks like the image below

Sorted List

2. Use LINQ
LINQ is only available from .NET framework 3.0 but in this post http://hintdesk.com/c-use-linq-on-net-framework-2-0/ I showed you how we can use it with .NET framework 2.0 and hence I can sort the list very fast when using LINQ without creating any class.

var vSort = from s in lstFile
			orderby Path.GetExtension(s), s ascending
			select s;

foreach (string strTemp in vSort)

The result is completely same with the output from first method

Sorted List

The advantage of LINQ in compare to IComparer<> is that we can use SQL Statement to make a sort algorithm on a simple object without creating any helper class. The complete source code of this example you can download here “Sort To File Extension

Slide.Show 2 – Configure to use it with Flickr

If you are a fan of WPF/Silverlight application, may be you know the open source slide show album http://slideshow2.codeplex.com/ . Slide.Show 2 is an open source Silverlight 2 control for publishing highly-customizable photo and video slide-shows on the Web. The most interesting feature of Slide.Show 2 is allowing us to display our albums which are stored in Flickr. That means I can upload my pictures to Flickr and use this Silverlight control to display them at my blog. Therefore I do not need to host any pictures at my host which costs a lot of disk space and do not have to back up anything. I push all risk to Flickr. Maybe you ask me why I don’t use Flash control with same features? The answer lies at “taste” and I am just a fan of Silverlight ^^.
So how can we use this control and embed it to our blog?

Continue reading Slide.Show 2 – Configure to use it with Flickr

WCF, Silverlight – Access WCF REST services with Silverlight

Windows Communication Foundation (WCF), part of the .NET Framework, provides the first unified programming model for rapidly building service-oriented applications. It enables the development of secure, reliable, transacted services that interoperate with current Microsoft investments and non-Microsoft platforms.

With the .NET Framework 3.5 release, WCF added support for building REST style services. REST, an architectural style for building distributed hypermedia driven applications, involves building resource-oriented services by defining resources that implement uniform interfaces using standard HTTP verbs (GET, POST, PUT, and DELETE), and that can be located/identified by a URI.

Continue reading WCF, Silverlight – Access WCF REST services with Silverlight