Advertisement

Sorting an array of strings in Win32?

Started by April 29, 2001 07:38 PM
2 comments, last by SikCiv 23 years, 9 months ago
Im using FindFirstFile and FindNextFile to retrieve the contents of a folder but to my knowledge FindFirstFile doesnt sort them in alphabetical order (unless there is a parameter I dont know about). Is there a Win32 function that will sort an array of strings in VC5 or 6? Or do I need to create my own sorting algorithm? Does FindFirstFileEx sort? Thanks.

  Downloads:  ZeroOne Realm

as far as I know you can use qsort. All you''ll need to do is write a small comparison function. One of the arguments to qsort will be a function pointer to your comparison function. I''m sure if you look up qsort in the docs you''ll find it.

kiev

Advertisement
Look around for info on the radix sort - if properly implemented, it''s far faster than the quicksort.
I don''t think there is a way of getting the files presorted using FindFirstFile() and FindNextFile(), but sorting using the STL is very simple.

  std::vector<std::string> files;// Fill In The File Information Herestd::sort(files.begin(), files.end);  


If you are using a more complex structure to store your files, you can pass in an optional predicate function (that you write) to sort the files by date, time, size, or whatever else you want.

  std::vector<YourFileStructure> files;// Fill In The File Information Herestd::sort(files.begin(), files.end, YourFunctionObject());  


In the code above, YourFunctionObject refers to a functional object, which is a way of encapsulating a function inside an object. Any good STL book will explain all of this. Personally, I like "STL Tutorial and Reference Guide."

This topic is closed to new replies.

Advertisement