There are several ways to sort files and folders, often by creating or modifying time, by file name. In C #, sorting by time and file name is very simple, with the array provided by the sorting method Array.Sort () a line of code can be done, of course, you can also use commonly used sorting methods, such as fast sorting, bubble sort and so on.
The method of sorting files also applies to folders, except that the variables passed are different. For ease of use, the C # file sorting and folder sorting are listed in four common methods: sorted by name and reverse order, chronological order and reverse order, respectively.
One, C # file sorting
1. ORDER BY name
<summary>
C # Sort by file name (order)
</summary>
<param name= "Arrfi" > array to sort </param>
private void Sortasfilename (ref fileinfo[] Arrfi)
{
Array.Sort (Arrfi, delegate (FileInfo x, FileInfo y) { returnx.Name.CompareTo (y.name); });
}
Call Method:
private void Sortfiles ()
{
String filePath = "e:\\";
DirectoryInfo di = new DirectoryInfo (FilePath);
fileinfo[] Arrfi = di. GetFiles ("* *");
Sortasfilename (ref Arrfi);
for (int i = 0; i < arrfi.length; i++)
Response.Write (Arrfi[i]. Name + ": <br/>");
}
The above code is the E-packing directory of all the files sorted, the code test passed, can be called directly.
2. In reverse order by name
<summary>
C # Sort by file name (reversed)
</summary>
<param name= "Arrfi" > array to sort </param>
private void Sortasfilename (ref fileinfo[] Arrfi)
{
Array.Sort (Arrfi, delegate (FileInfo x, FileInfo y) { returny.Name.CompareTo (x.name); });
}
The calling method is the same as the order, not an example.
3. Chronological order by creation time
<summary>
C # Sort by creation time (order)
</summary>
<param name= "Arrfi" > array to sort </param>
private void Sortasfilecreationtime (ref fileinfo[] Arrfi)
{
Array.Sort (Arrfi, delegate (FileInfo x, FileInfo y) { returnx.CreationTime.CompareTo (y.creationtime); });
}
The calling method is the same as the previous.
4. In reverse chronological order of creation time
<summary>
C # Sort by creation time (reversed)
</summary>
<param name= "Arrfi" > array to sort </param>
private void Sortasfilecreationtime (ref fileinfo[] Arrfi)
{
Array.Sort (Arrfi, delegate (FileInfo x, FileInfo y) { returny.CreationTime.CompareTo (x.creationtime); });
}
The calling method is the same as the previous.
Second, C # folder sorting
1. Order BY folder name
<summary>
C # Sort by folder name (order)
</summary>
<param name= "dirs" > pending folder array </param>
private void Sortasfoldername (ref directoryinfo[] dirs)
{
Array.Sort (dirs, delegate (DirectoryInfo x, DirectoryInfo y) { returnx.Name.CompareTo (y.name); });
}
Call Method:
private void Foldersort ()
{
String filePath = "e:\\";
DirectoryInfo di = new DirectoryInfo (FilePath);
directoryinfo[] Arrdir = di. GetDirectories ();
Sortasfoldername (ref arrdir);
for (int i = 0; i < arrdir.length; i++)
Response.Write (Arrdir[i]. Name + ": <br/>");
}
The code above is a list of all folders under the E-packing directory in the order of name, and the code is tested through Visual Studio 2010.
2, by folder name in reverse order
<summary>
C # Sort by folder name (reversed)
</summary>
<param name= "dirs" > pending folder array </param>
private void Sortasfoldername (ref directoryinfo[] dirs)
{
Array.Sort (dirs, delegate (DirectoryInfo x, DirectoryInfo y) { returny.Name.CompareTo (x.name); });
}
3. Order BY Folder creation time
<summary>
C # Sort by folder folders creation time (order)
</summary>
<param name= "dirs" > pending folder array </param>
private void Sortasfoldercreationtime (ref directoryinfo[] dirs)
{
Array.Sort (dirs, delegate (DirectoryInfo x, DirectoryInfo y) { returnx. Creationtime.compareto (Y.creationtime); });
}
4, by folder creation time in reverse order
<summary>
C # Sort by folder creation time (reverse)
</summary>
<param name= "dirs" > pending folder array </param>
private void Sortasfoldercreationtime (ref directoryinfo[] dirs)
{
Array.Sort (dirs, delegate (DirectoryInfo x, DirectoryInfo y) { returny. Creationtime.compareto (X.creationtime); });
}
If you want to sort by the last modified time of the file or folder, the method is the same, just change the creationtime to LastWriteTime.
C # files and file folders sorted by time, name-order and reverse