Fri, 17 May 2024


How to work with Comparer in order to sort files by date?

By: Anton, NetArt Media
Sat, 22 January 2022

using System;
using System.Collections;
using System.IO;

namespace InfoSystem
{
/// <summary>
/// This class is created by Anton Zamov
/// For more information about Anton Zamov
/// and a lot of other useful examples
/// please visit http://netartmedia.net
/// You may use and redistribute this code as
/// long as you keep this message intact.
/// </summary>
public class FilesDateComparer: IComparer
{
public int Compare (object x, object y)
{

int iResult;

FileInfo oFileX=(FileInfo)x;
FileInfo oFileY=(FileInfo)y;

if(oFileX.LastWriteTime==oFileY.LastWriteTime)
{
iResult=0;
}
else
if(oFileX.LastWriteTime>oFileY.LastWriteTime)
{
iResult=1;
}
else
{
iResult=-1;
}


return iResult;
}

}
}


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox