sorting files retrieved by OpenFileDialog

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm not happy with the order in which OpenFileDialog retrieves multiple
selected files. I want them in Date order, oldest to newest, but by default
they come in by filename, last to first.

The only property that seems relevant is Sort, and all that does is invert
the filename order. I need Date order. I've searched for an answer but
nothing has come up. Any ideas?
 
See if the following helps
NOTE: Havent compiled it, so might have typo or syntax errors

' Comparer Class
Public Class CompareFileDates
Implements IComparer
Function IComparer.Compare(ByVal a As Object, ByVal b As Object) As Integer
Return DateTime.Compare( System.Io.File.GetLastWriteTime(DirectCast(a,
String)) , System.Io.File.GetLastWriteTime(DirectCast(b, String)) )
End Function
End Class

' Code to sort the files by date
Array.Sort(files, new CompareFileDates());

HTH
rawCoder
 
I'm not happy with the order in which OpenFileDialog retrieves
multiple selected files. I want them in Date order, oldest to newest,
but by default they come in by filename, last to first.

The only property that seems relevant is Sort, and all that does is
invert the filename order. I need Date order. I've searched for an
answer but nothing has come up. Any ideas?

I think the anwser would be to create two arrays and add the selected files
to one array as FileInfo(path) and the other array add FileInfo
(path).DateCreated for each item returned, then do [Array].Sort
(dateArray,fileInfoArray), the resulting fileInfoArray will be sorted by
dateArray. More on this here:http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/cpref/html/frlrfsystemarrayclasssorttopic.asp

The other way would be to implement IComparer in a class (which is only one
function) throw all the selected files in an array as FileInfo's and then
[Array].Sort(fileInfoArray,ComparerClass) More info
here:http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemcollectionsicomparerclasstopic.asp
 
Thanks for the quick response!

That's foreign functionality to me and I received a number of errors after
inserting the code so I'm obviously going to have to play with it a bit. I
appreciate you getting me started!

Randall
 
Thanks, I'll try that as well.

Boggles my mind that MS didn't include a property for this on the
FileOpenDialog.

Randall Arnold

MeltingPoint said:
I'm not happy with the order in which OpenFileDialog retrieves
multiple selected files. I want them in Date order, oldest to newest,
but by default they come in by filename, last to first.

The only property that seems relevant is Sort, and all that does is
invert the filename order. I need Date order. I've searched for an
answer but nothing has come up. Any ideas?

I think the anwser would be to create two arrays and add the selected files
to one array as FileInfo(path) and the other array add FileInfo
(path).DateCreated for each item returned, then do [Array].Sort
(dateArray,fileInfoArray), the resulting fileInfoArray will be sorted by
dateArray. More on this here:http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/cpref/html/frlrfsystemarrayclasssorttopic.asp

The other way would be to implement IComparer in a class (which is only one
function) throw all the selected files in an array as FileInfo's and then
[Array].Sort(fileInfoArray,ComparerClass) More info
here:http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemcollectionsicomparerclasstopic.asp
 
I would modify RawCoder's code a bit and it might work:

' Comparer Class
Public Class CompareFileDates
Implements IComparer
Function IComparer.Compare(ByVal a As Object, ByVal b As Object) As Integer

Return DateTime.Compare(
DirectCast(System.Io.File.GetLastWriteTime(DirectCast(a,String)), String)
String)) , DirectCast(System.Io.File.GetLastWriteTime(DirectCast(b, String))
,string) )
End Function
End Class

' Code to sort the files by date
Array.Sort(files, new CompareFileDates());
 
That's really saying "Why didn't MS include a property for everything one
could ever think of even if it hasn't been thought of yet.".

The purpose of the FileOpenDialog has been well established for many years.
It is to return 1 or more filenames. It is not to return 1 or more objects
representing information about file(s).

The whole concept of a framework is a foundation upon which you build. MS
have provided the foundation and it's up to you to build upon it to achieve
the desired results. In a nutshell, that's what programming is.


Randall Arnold said:
Thanks, I'll try that as well.

Boggles my mind that MS didn't include a property for this on the
FileOpenDialog.

Randall Arnold

MeltingPoint said:
I'm not happy with the order in which OpenFileDialog retrieves
multiple selected files. I want them in Date order, oldest to newest,
but by default they come in by filename, last to first.

The only property that seems relevant is Sort, and all that does is
invert the filename order. I need Date order. I've searched for an
answer but nothing has come up. Any ideas?

I think the anwser would be to create two arrays and add the selected
files
to one array as FileInfo(path) and the other array add FileInfo
(path).DateCreated for each item returned, then do [Array].Sort
(dateArray,fileInfoArray), the resulting fileInfoArray will be sorted by
dateArray. More on this
here:http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/cpref/html/frlrfsystemarrayclasssorttopic.asp

The other way would be to implement IComparer in a class (which is only
one
function) throw all the selected files in an array as FileInfo's and then
[Array].Sort(fileInfoArray,ComparerClass) More info
here:http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemcollectionsicomparerclasstopic.asp
 
With all due respect, that's a rather disingenuous response. No, my question
was NOT in the manner you allege; I simply asked about ONE property, whch in
no way leads to your hyperbolic sarcasm.

And while the functionality of the dialog control may have remained static
for many years, that's not a reason for it to always remain that way. I note
that the .NET framework both added to and took away from many controls.
Obviously MS doesn't consider the property set to be cast in stone. In my
*opinion* (which is just as valid as yours, by the way), the functionality I
wished for would be easy enough for MS to implement and would simplify what
is obviously a cumbersome workaround; note that none of the suggestions
mentioned here has worked for my purposes.

If you don't have anything productive to contribute to my queries or
comments, I ask you just not add anything at all. Thanks.

Stephany Young said:
That's really saying "Why didn't MS include a property for everything one
could ever think of even if it hasn't been thought of yet.".

The purpose of the FileOpenDialog has been well established for many years.
It is to return 1 or more filenames. It is not to return 1 or more objects
representing information about file(s).

The whole concept of a framework is a foundation upon which you build. MS
have provided the foundation and it's up to you to build upon it to achieve
the desired results. In a nutshell, that's what programming is.


Randall Arnold said:
Thanks, I'll try that as well.

Boggles my mind that MS didn't include a property for this on the
FileOpenDialog.

Randall Arnold

MeltingPoint said:
"=?Utf-8?B?UmFuZGFsbCBBcm5vbGQ=?="

I'm not happy with the order in which OpenFileDialog retrieves
multiple selected files. I want them in Date order, oldest to newest,
but by default they come in by filename, last to first.

The only property that seems relevant is Sort, and all that does is
invert the filename order. I need Date order. I've searched for an
answer but nothing has come up. Any ideas?

I think the anwser would be to create two arrays and add the selected
files
to one array as FileInfo(path) and the other array add FileInfo
(path).DateCreated for each item returned, then do [Array].Sort
(dateArray,fileInfoArray), the resulting fileInfoArray will be sorted by
dateArray. More on this
here:http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/cpref/html/frlrfsystemarrayclasssorttopic.asp

The other way would be to implement IComparer in a class (which is only
one
function) throw all the selected files in an array as FileInfo's and then
[Array].Sort(fileInfoArray,ComparerClass) More info
here:http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemcollectionsicomparerclasstopic.asp
 
Has anyone figured this out yet? I just got Vista and find it horribly
frustrating that I can't open a folder from inside a program and sort by
date...only file name...crazy!

MS must have fixed this by now (XP and all other previous OS versions did
this without a question)

Thx!

Jen in SJ
 
Has anyone figured this out yet?  I just got Vista and find it horribly
frustrating that I can't open a folder from inside a program and sort by
date...only file name...crazy!

MS must have fixed this by now (XP and all other previous OS versions did
this without a question)

Thx!

Jen in SJ

3 year-old topic, Wow...
 
Back
Top