Click here to Skip to main content
Click here to Skip to main content

FAT-32 Sorter

By , 29 May 2012
 

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)

About the Author

Olekandr M
Technical Lead
Ireland Ireland
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberenhzflep29 May '12 - 22:20 
Forgotten all about that problem, its been a few years. Had a similar problem with the music player in my android phone. It plays the tracks alphabetically. :grr:

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

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