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! :)

The two tests you posted look like two different directories -- they are not even close to listing the same files.

They are the same directory, only there's something like 926 entries in that directory so I cut it short :S

Only ls sorts them in a different way: the following directories

A Perfect Circle - Emotive (2004)
A Perfect Circle - Mer De Noms (2000)
A Perfect Circle - Thirteenth Step (2003)
A Toys Orchestra - Technicolor Dreams (2007)

for example are sorted by my program with the beginning of the 'A', while ls puts them right before the 'B' - it looks like in the sorting my program considers a white space to be "<" than 'a' while ls does not.

If you need the complete output I can provide it, only it's huge ^^"

I made a test with a dummy folder to explain myself better. ls /home/ludovico/Desktop/Test/ output:

Aaaa  
A aaa  
Abcd  
A bcd

./ScanDirectoryTest /home/ludovico/Desktop/Test/ output:

A aaa
A bcd
Aaaa
Abcd

Hope that helps in making myself clear ^^"

The ls command is not sorting alphabetically using standard ascii decimal values. If you look at a standard ascii chart you will see that a space has a lower decimal value than any letter, and that's why "A aa" is before "Aaa". Maybe its sorting by file creation time or something like that.

Mmm thank you :)

Now I see the reason why it behaves this way.
Only thing left to do is to understand how to write my program so that it behaves like this too :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.