Click here to Skip to main content
Click here to Skip to main content
Alternative Article

FAT-32 Sorter

, 29 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:  

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). 

 

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

Hide pointer operations using STL

by replacing 

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

with 

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

Remove leading '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);  

Entire new method of compareEntries

// 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)

Share

About the Author

Olekandr M
Technical Lead
Ireland Ireland
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberenhzflep29-May-12 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 29 May 2012
Article Copyright 2012 by Olekandr M
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid