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.
- }