I have files in directory like that
0-0.jpeg
0-1.jpeg
0-5.jpeg
0-9.jpeg
0-10.jpeg
0-12.jpeg
....
when i loading files:
See the "CustomSort" function here.
List<string> list = new List<string>() {
"0-5.jpeg",
"0-9.jpeg",
"0-0.jpeg",
"0-1.jpeg",
"0-10.jpeg",
"0-12.jpeg"};
list.CustomSort().ToList().ForEach(x => Console.WriteLine(x));
Its output:
0-0.jpeg
0-1.jpeg
0-5.jpeg
0-9.jpeg
0-10.jpeg
0-12.jpeg
Alphabetically, the "wrong" order is in fact correct. If you want it sorted numerically then you'll need to either:
See the answer to Sorting Directory.GetFiles() for an example of #3.
Here my solution. I first parsed the file path, then defined the order rules.
For the following code you need the namespace System.Text.RegularExpressions
.
Regex parseFileNameForNaturalSortingRegex = new Regex(
@"
^
(?<DirectoryName>.*)
(?<FullFileName>
(?<FileNameBasePart>[^/\\]*)
(?<FileNameNumericPart>\d+)?
(?>
\.
(?<FileNameExtension>[^./\\]+)
)?
)
$
",
RegexOptions.IgnorePatternWhitespace | RegexOptions.RightToLeft
);
FileInfo[] filesOrdered = System.IO.Directory.GetFiles(@"C:\my\source\directory")
.Select(fi =>
{
Match match = parseFileNameForNaturalSortingRegex.Match(fi.FullName);
return new
{
FileInfo = fi,
DirectoryName = match.Groups["DirectoryName"].Value,
FullFileName = match.Groups["FullFileName"].Value,
BasePart = match.Groups["FileNameBasePart"].Value,
NumericPart = match.Groups["FileNameNumericPart"].Success ? int.Parse(match.Groups["FileNameNumericPart"].Value) : -1,
HasFileNameExtension = match.Groups["FileNameExtension"].Success,
FileNameExtension = match.Groups["FileNameExtension"].Value
};
})
.OrderBy(r => r.DirectoryName)
.ThenBy(r => r.BasePart)
.ThenBy(r => r.NumericPart)
.ThenBy(r => r.HasFileNameExtension)
.ThenBy(r => r.FileNameExtension)
.Select(r => r.FileInfo)
.ToArray();
Implement Comparison method for your specific case and use it in Array.Sort
.
private int CompareByNumericName(FileInfo firstFile, FileInfo secondFile)
{
/* First remove '0-' and '.jpeg' from both filenames then... */
int firstFileNumericName = Int32.Parse(firstFile.Name);
int secondFileNumericName = Int32.Parse(secondFile.Name);
return firstFileNumericName.CompareTo(secondFileNumericName);
}
FileInfo[] files = di.GetFiles();
Array.Sort<FileInfo>(files, CompareByNumericName);
For solving this problem you can use StrCmpLogicalW windows API.
For more details see This Artice.
You filenames appear to be structured. If you just sort them, they sort as ordinary strings. You need to:
Personally, I'd create a class that represented the structure implicit in the filename. Perhaps it should wrap the FileInfo
. The constructor for that class should parse the filename into its constituent parts and instantiate the properties of the class appropriately.
The class should implement IComparable
/IComparable<T>
(or you could create an implementation of Comparer).
Sort your objects and they should then come out in the collating sequence you desire.
If looks like your file names are composed of 3 parts:
So your class might look something like
public class MyFileInfoWrapper : IComparable<MyFileInfoWrapper>,IComparable
{
public MyFileInfoWrapper( FileInfo fi )
{
// your implementation here
throw new NotImplementedException() ;
}
public int Hi { get ; private set ; }
public int Lo { get ; private set ; }
public string Extension { get ; private set ; }
public FileInfo FileInfo { get ; private set ; }
public int CompareTo( MyFileInfoWrapper other )
{
int cc ;
if ( other == null ) cc = -1 ;
else if ( this.Hi < other.Hi ) cc = -1 ;
else if ( this.Hi > other.Hi ) cc = +1 ;
else if ( this.Lo < other.Lo ) cc = -1 ;
else if ( this.Lo > other.Lo ) cc = +1 ;
else cc = string.Compare( this.Extension , other.Extension , StringComparison.InvariantCultureIgnoreCase ) ;
return cc ;
}
public int CompareTo( object obj )
{
int cc ;
if ( obj == null ) cc = -1 ;
else if ( obj is MyFileInfoWrapper ) cc = CompareTo( (MyFileInfoWrapper) obj ) ;
else throw new ArgumentException("'obj' is not a 'MyFileInfoWrapper' type.", "obj") ;
return cc ;
}
}