Tuesday, November 02, 2010

Sorting SharePoint Folder Contents

Recently I needed to sort the contents of a SharePoint folder, in which I used IComparer to get my task done. Following is the code I used. Hope it is helpful.

As you see below I am using the TimeCreated property of SPFile object to do the comparison, other than this you can use any other property of the SPFile object. For a list of available properties refer MSDN.





  1. // Comparer Class.
  2. public class SPFilesComparer : IComparer
  3. {
  4.     #region IComparer Members
  5.     public int Compare(object object1, object object2)
  6.     {
  7.         return new CaseInsensitiveComparer().Compare(((SPFile)object1).TimeCreated, ((SPFile)object2).TimeCreated);
  8.     }
  9.     #endregion
  10. }








  1. ArrayList fileList = new ArrayList(attachFiles);
  2. IComparer compFiles = new SPFilesComparer();
  3. // Do the sorting based on the SPFilesComparer.
  4. fileList.Sort(compFiles);
  5. foreach (SPFile attachFile in fileList)
  6. {
  7.     // Use the sorted list.
  8. }




4 comments:

Anonymous said...

Hi. Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

Arjuna said...

Sorry for the delayed reply, You can take whatever needed from my posts as it is, as long as you put the url or mention about my original post.

Anonymous said...

Hello Thanks for such a perfect submit and the evaluation, I'm completely impressed! Maintain stuff like this coming.

Anonymous said...

your site is exceptionally superb!!!