Hi all.
I am playing around with this and this code snippet by Ancient Dragon.
I didn't want to copy-paste so I came out with various simple attempts, the last of which looks like this:
#include <iostream>
#include <unistd.h>
#include <dirent.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[]) {
DIR *mypath;
dirent *cd;
vector<string> ls;
if(argc<2) {
return EXIT_FAILURE;
}
string path = argv[1];
mypath = opendir(path.c_str());
if(!mypath) {
cout << endl << "Could not open directory " << path << endl;
return EXIT_FAILURE;
}
cout << endl;
while((cd = readdir(mypath))) {
string name = cd->d_name;
if(name!="."&&name!="..") {
ls.push_back(name);
}
}
sort(ls.begin(), ls.end());
unsigned int i;
cout << endl << endl;
for(i=0; i<ls.size();i++) {
cout << endl << ls[i];
}
cout << endl << endl;
closedir(mypath);
return EXIT_SUCCESS;
}
So now I am comparing the output of ./ScanDirectoryTest /media/Volume/Music/
and ls /media/Volume/Music/
and I found out that they sort in different ways: ls outputs like this
4 Non Blondes - Bigger, Better, Faster, More! (1992)
Ac Dc - Back In Black (1980)
Adagio - Underworld (2003)
Adem - Love And Other Planets (2006)
Aerosmith - O, Yeah! Ultimate Aerosmith Hits (2002)
Afghan Wighs - Gentlemen (1993)
...
and my program like this:
4 Non Blondes - Bigger, Better, Faster, More! (1992)
A Perfect Circle - Emotive (2004)
A Perfect Circle - Mer De Noms (2000)
A Perfect Circle - Thirteenth Step (2003)
A Toys Orchestra - Technicolor Dreams (2007)
Ac Dc - Back In Black (1980)
...
You can see that my test puts "A Perfect Circle" ('A' followed by a space) before "Ac Dc", while ls does not.
My question is: how can I emulate ls's method of sorting?
I thought to write my own version of operator< and to pass it to the sort algorithm, but I'm not sure if this is even possible. :s
Anyway, anticipated thanks and sorry for eventual english mistakes! :)