Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Objective-C

FAT-32 Sorter

4.83/5 (3 votes)
29 May 2012CPOL1 min read 19.6K   20  
Update to FAT-32 Sorter helping to ignore leading 'the ' when sorting

Introduction 

Added command line argument to ignore leading 'the ' when comparing files and folder names.

Background 

There are few car owners complaining about their head units that do not respect alphabetical order of the files and folders on USB drive or MP3 player but follow their physical locations (including recent models of Honda and Toyota). Great work done by this project is particularly useful to fix the above. When applied this logic to music folders, it is nice to ignore leading 'the ' as it is done by many music players automatically.

Using the code  

Command line (1st change)

Added check for ignoreThe command line argument in function main and set global flag ignoreTheInFolderCompare:  

C++
std::vector<tstring> params(argv, argv+argc);
tstring param(_T("ignoreThe"));
if(std::find(params.begin(), params.end(), param) != params.end())
	ignoreTheInFolderCompare = true;  

Added feature (2nd change)

Below diagram shows original sequence of sorting the directory entries. This sequence did not change but last step implementation extended to use above command line argument and global flag (highlighted in orange). 

Image 1 

comapreEntries method is rewritten to implement the above feature plus general improvements as described below.

Hide pointer operations using STL

by replacing 

C++
WCHAR* o1Name = o1->getName();
...
bool ret = (_wcsicmp(o1Name, o2Name) <= 0); 

with 

C++
wstring o1Name(o1->getName());
...
bool ret = (o1Name.compare(o2Name) <= 0);

Remove leading 'the ' 

C++
if (o1Name.length() > 4 && !o1Name.compare(0, prefix.size(), prefix))
    o1Name = o1Name.substr(4);
if (o2Name.length() > 4 && !o2Name.compare(0, prefix.size(), prefix))
    o2Name = o2Name.substr(4);  

Entire new method of compareEntries

C++
// This utility function is used by the std::sort function. 
// This function help to determine if two entries are in the right order, or needed to be switched
bool compareEntries( CEntry* o1, CEntry* o2)
{
    wstring o1Name(o1->getName());
    wstring o2Name(o2->getName());

    if(ignoreTheInFolderCompare)
    {
        transform(o1Name.begin(), o1Name.end(), o1Name.begin(), tolower);
        transform(o2Name.begin(), o2Name.end(), o2Name.begin(), tolower);
        wstring prefix(L"the ");
        if (o1Name.length() > 4 && !o1Name.compare(0, prefix.size(), prefix))
            o1Name = o1Name.substr(4);
        if (o2Name.length() > 4 && !o2Name.compare(0, prefix.size(), prefix))
            o2Name = o2Name.substr(4);
    }

    // Returns "true" if the entries are in the right order (o1 should be before o2)
    bool ret = (o1Name.compare(o2Name) <= 0);

    return ret;
}

Points of Interest   

I created this post on blogger sharing another utility helping to play your music on the "dummy" car stereo hassle free. This project was essential for getting things working nicely in my car so hope others will appreciate too.

History 

N/A

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)