<p><code>System.IO.Directory.GetFiles()</code> returns a <code>string[]</code>.  What is the default sort order for the returned values?  I'm assuming by name, but if so how much does the current culture effect it?  Can you change it to something like creation date? </p>

<p><strong>Update:</strong> MSDN points out that the sort order is not guaranteed for .Net 3.5, but the 2.0 version of the page doesn't say anything at all and neither page will help you sort by things like creation or modification time.  That information is lost once you have the array (it contains only strings).  I could build a comparer that would check for each file it gets, but that means accessing the file system repeatedly when presumably the .GetFiles() method already does this.  Seems very inefficient.</p>

[{'answer_id': 52847, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 3, 'selected': False, 'text': '<p></p>\n\n<pre class="lang-vb prettyprint-override"><code>Dim Files() As String\nFiles = System.IO.Directory.GetFiles("C:\\")\nArray.Sort(Files)\n</code></pre>\n'}, {'answer_id': 52850, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 3, 'selected': False, 'text': '<p>From <a href="http://msdn.microsoft.com/en-us/library/wz42302f.aspx" rel="noreferrer">msdn</a>:</p>\n\n<blockquote>\n  <p>The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.</p>\n</blockquote>\n\n<p>The <a href="http://msdn.microsoft.com/en-us/library/system.array.sort.aspx" rel="noreferrer">Sort()</a> method is the standard Array.Sort(), which takes in IComparables (among other overloads), so if you sort by creation date, it will handle localization based on the machine settings.</p>\n'}, {'answer_id': 52852, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/wz42302f.aspx" rel="nofollow noreferrer">MSDN Documentation</a> states that there is no guarantee of any order on the return values. You have to use the Sort() method.</p>\n'}, {'answer_id': 52854, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 2, 'selected': False, 'text': "<p>You are correct, the default is my name asc. The only way I have found to change the sort order it to create a datatable from the FileInfo collection.</p>\n\n<p>You can then used the DefaultView from the datatable and sort the directory with the .Sort method.</p>\n\n<p>This is quite involve and fairly slow but I'm hoping someone will post a better solution.</p>\n"}, {'answer_id': 52859, 'author': 'Tina Marie', 'author_id': 4666, 'author_profile': 'https://Stackoverflow.com/users/4666', 'pm_score': 2, 'selected': False, 'text': '<p>You could write a custom IComparer interface to sort by creation date, and then pass it to Array.Sort.  You probably also want to look at StrCmpLogical, which is what is used to do the sorting Explorer uses (sorting numbers correctly with text).</p>\n'}, {'answer_id': 52861, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to sort by something like creation date you probably need to use DirectoryInfo.GetFiles and then sort the resulting array using a suitable Predicate.</p>\n'}, {'answer_id': 52865, 'author': 'Vertigo', 'author_id': 5468, 'author_profile': 'https://Stackoverflow.com/users/5468', 'pm_score': 2, 'selected': False, 'text': '<p>You can implement custom iComparer to do sorting. Read the file info for files and compare as you like. </p>\n\n<pre><code>IComparer comparer = new YourCustomComparer();\nArray.Sort(System.IO.Directory.GetFiles(), comparer);\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.collections.icomparer(VS.80).aspx" rel="nofollow noreferrer">msdn info IComparer interface</a></p>\n'}, {'answer_id': 52867, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 8, 'selected': True, 'text': '<p>If you\'re interested in properties of the files such as CreationTime, then it would make more sense to use System.IO.DirectoryInfo.GetFileSystemInfos(). \nYou can then sort these using one of the extension methods in System.Linq, e.g.:</p>\n\n<pre><code>DirectoryInfo di = new DirectoryInfo("C:\\\\");\nFileSystemInfo[] files = di.GetFileSystemInfos();\nvar orderedFiles = files.OrderBy(f =&gt; f.CreationTime);\n</code></pre>\n\n<p>Edit - sorry, I didn\'t notice the .NET2.0 tag so ignore the LINQ sorting. The suggestion to use System.IO.DirectoryInfo.GetFileSystemInfos() still holds though.</p>\n'}, {'answer_id': 149100, 'author': 'sebastiaan', 'author_id': 5018, 'author_profile': 'https://Stackoverflow.com/users/5018', 'pm_score': 4, 'selected': False, 'text': '<p>Here\'s the VB.Net solution that I\'ve used.</p>\n\n<p>First make a class to compare dates:</p>\n\n<pre><code>Private Class DateComparer\n    Implements System.Collections.IComparer\n\n    Public Function Compare(ByVal info1 As Object, ByVal info2 As Object) As Integer Implements System.Collections.IComparer.Compare\n        Dim FileInfo1 As System.IO.FileInfo = DirectCast(info1, System.IO.FileInfo)\n        Dim FileInfo2 As System.IO.FileInfo = DirectCast(info2, System.IO.FileInfo)\n\n        Dim Date1 As DateTime = FileInfo1.CreationTime\n        Dim Date2 As DateTime = FileInfo2.CreationTime\n\n        If Date1 &gt; Date2 Then Return 1\n        If Date1 &lt; Date2 Then Return -1\n        Return 0\n    End Function\nEnd Class\n</code></pre>\n\n<p>Then use the comparer while sorting the array:</p>\n\n<pre><code>Dim DirectoryInfo As New System.IO.DirectoryInfo("C:\\")\nDim Files() As System.IO.FileInfo = DirectoryInfo.GetFiles()\nDim comparer As IComparer = New DateComparer()\nArray.Sort(Files, comparer)\n</code></pre>\n'}, {'answer_id': 279142, 'author': 'Chris Karcher', 'author_id': 2773, 'author_profile': 'https://Stackoverflow.com/users/2773', 'pm_score': 4, 'selected': False, 'text': "<p>In .NET 2.0, you'll need to use Array.Sort to sort the FileSystemInfos.</p>\n\n<p>Additionally, you can use a Comparer delegate to avoid having to declare a class just for the comparison:</p>\n\n<pre><code>DirectoryInfo dir = new DirectoryInfo(path);\nFileSystemInfo[] files = dir.GetFileSystemInfos();\n\n// sort them by creation time\nArray.Sort&lt;FileSystemInfo&gt;(files, delegate(FileSystemInfo a, FileSystemInfo b)\n                                    {\n                                        return a.LastWriteTime.CompareTo(b.LastWriteTime);\n                                    });\n</code></pre>\n"}, {'answer_id': 2555123, 'author': 'Mehdi Anis', 'author_id': 208325, 'author_profile': 'https://Stackoverflow.com/users/208325', 'pm_score': 1, 'selected': False, 'text': '<p>Just an idea. I like to find an easy way out and try re use already available resources. if I were to sort files I would\'ve just create a process and make syscal to "DIR [x:\\Folders\\SubFolders*.*] /s /b /on" and capture the output. </p>\n\n<p>With system\'s DIR command you can sort by :</p>\n\n<pre><code>/O          List by files in sorted order.\nsortorder    N  By name (alphabetic)       S  By size (smallest first)\n             E  By extension (alphabetic)  D  By date/time (oldest first)\n             G  Group directories first    -  Prefix to reverse order\n\nThe /S switch includes sub folders\n</code></pre>\n\n<p>I AM NOT SURE IF D = By Date/Time is using LastModifiedDate or FileCreateDate. But if the needed sort order is already built-in in the DIR command, I will get that by calling syscall. And it\'s FAST. I am just the lazy guy ;)</p>\n\n<p><em>After a little googling I found switch to sort by particular date/time:-</em></p>\n\n<pre><code>/t [[:]TimeField] : Specifies which time field to display or use for sorting. The following list describes each of the values you can use for TimeField. \n\nValue Description \nc : Creation\na : Last access\nw : Last written\n</code></pre>\n'}, {'answer_id': 15544428, 'author': 'Simon Molloy', 'author_id': 942604, 'author_profile': 'https://Stackoverflow.com/users/942604', 'pm_score': 1, 'selected': False, 'text': '<p>A more succinct VB.Net version, if anyone is interested</p>\n\n<pre class="lang-vb prettyprint-override"><code>Dim filePaths As Linq.IOrderedEnumerable(Of IO.FileInfo) = _\n  New DirectoryInfo("c:\\temp").GetFiles() _\n   .OrderBy(Function(f As FileInfo) f.CreationTime)\nFor Each fi As IO.FileInfo In filePaths\n  \' Do whatever you wish here\nNext\n</code></pre>\n'}, {'answer_id': 20548522, 'author': 'skeltech', 'author_id': 3096197, 'author_profile': 'https://Stackoverflow.com/users/3096197', 'pm_score': 2, 'selected': False, 'text': "<p>A more succinct VB.Net version...is very nice. Thank you. \nTo traverse the list in reverse order, add the reverse method...</p>\n\n<pre><code>For Each fi As IO.FileInfo In filePaths.reverse\n  ' Do whatever you wish here\nNext\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/52842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3043/']