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.
- // Comparer Class.
- public class SPFilesComparer : IComparer
- {
- #region IComparer Members
- public int Compare(object object1, object object2)
- {
- return new CaseInsensitiveComparer().Compare(((SPFile)object1).TimeCreated, ((SPFile)object2).TimeCreated);
- }
- #endregion
- }
- ArrayList fileList = new ArrayList(attachFiles);
- IComparer compFiles = new SPFilesComparer();
- // Do the sorting based on the SPFilesComparer.
- fileList.Sort(compFiles);
- foreach (SPFile attachFile in fileList)
- {
- // Use the sorted list.
- }
4 comments:
Hi. Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?
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.
Hello Thanks for such a perfect submit and the evaluation, I'm completely impressed! Maintain stuff like this coming.
your site is exceptionally superb!!!
Post a Comment