#6 numerical file sort

open
None
5
2012-09-06
2006-05-26
Dennis Lim
No

This patch is to sort files more intelligently
standard ASCII comparison is used most of the time.
However when a numerical digit is encountered on both
strings, then comparison is performed by converting
into int and comparing the numbers. This solves
problems sorting files like file1, file10, file 2,...
Apparently Windows XP is smart enough to follow this
algo already in 'Explorer'.

CSDiff format patch as follows

898a899,945

static int CompareStringNum(const wxString& first,
const wxString& second)
{
int firstCt = 0;
int secondCt = 0;
//assume that wxString has properly null terminated
strings.
for(;;)
{
char firstChar = first[firstCt++];
char secondChar = second[secondCt++];
int firstNum = 0;
int secondNum = 0;

  if( firstChar>='0' && firstChar<='9' &&

secondChar>='0' && secondChar<='9')
{
do
{
firstNum=10;
firstNum+= firstChar-'0';
firstChar = first[firstCt++];
}
while(firstChar>='0' && firstChar<='9');
do
{
secondNum
=10;
secondNum+= secondChar-'0';
secondChar = second[secondCt++];
}
while (secondChar>='0' && secondChar<='9');
int diff = firstNum-secondNum;
if(diff!=0)
{
return diff;
}
}
if(firstChar=='\0'&&secondChar=='\0')
{
return 0;
}
int diff = firstChar-secondChar;
if(diff!=0)
{
return diff;
}
}
return 0; // will not execute. depends on one of the
return statements in the for(ever) loop.
}

901c948
< Filenames->Sort();


Filenames->Sort(CompareStringNum);

Discussion

  • Nobody/Anonymous

    Logged In: NO

    No, this is not correct at all, you are severely confused.
    You do not hand sort items, you let the OS sort it based on
    its locale. In linux, it looks at LC_COLLATE to determine
    how exactly it should go about, then uses a standardized
    method to sort. This patch will severely mess up the
    sorting. Just because you think the sorting is wrong because
    it's not "windows-like" does not mean it is wrong.

     
  • Dennis Lim

    Dennis Lim - 2006-05-29

    Logged In: YES
    user_id=117202

    Sorry mate, you're smarter than me. I don't know about
    LC_LOCALE and such. However, Windows does sort it correctly
    in Explorer but Comical does not. I am observing that some
    comics do not display pages in the correct order. That makes
    it wrong in my book. My patch will allow the comics to be
    displayed in the correct order.

    Perhaps my patch does not fix it in the LC_LOCALE friendly
    manner but the bug does exist and the patch does fix the
    bug. If yourself or anyone else would supply a fix that
    takes the LC_LOCALE into account, I would appreciate it. In
    other words, I'm open to suggestions.

    The default may revert to OS specified sorting. That would
    be great for uniformity in sorting method across
    applications. However, for application context specific
    sorting, it makes sense for us to hand code it.

    Tracing the wxArrayString codes also leads to effectively
    using a memcmp operation. This doesn't look like a locale
    specific operation.

    Then again, I might be severly confused. Could you enlighten
    me?

     
  • Nobody/Anonymous

    Logged In: NO

    Sorry, you're right. I had too much faith in wx's ability to
    sort properly it seems. Looking at their code for sorting
    all they do is a simple memcmp like you said (they even
    mention at one point in the code "maybe we should use
    wxStrcoll?"....geez). The proper way to do it would be to
    use strcoll which looks at the OS's locale for how to sort
    (and I guess wxStrcoll if that even exists). Maybe try using
    one of them since if windows sorts like that then maybe
    those funcs will do it the way you want and you won't need
    all that code. But whatever, it doesn't matter to me now.

     

Log in to post a comment.