[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

RE: RE: Repobrowser - Numeric order filename sorting

From: Gerasimov, Ivan <Ivan.Gerasimov_at_transas.com>
Date: 2006-04-11 10:46:20 CEST

Hello!

> > I think that optional numerical filename sorting would be a
> > great thing in
> > the repobrowser(maybe as the default sorting aswell).
> >
> > I am wondering if somebody else finds this useful?

> Useful yes, but that feature doesn't come for free. You would need a
> parser that intelligently detects which part of a string is numerical and
> then apply numerical sorting to these bits, after having sorted 'by text'
> first.
> Too much effort for just a little gain.
> If you mix text and numbers like in your example, you would need another
> sort/compare step for each change in the sequence. Something like v1.3.8,
> v1.3.18 would be parsed into: string-number-string-number-string-number.
> Complicated.

The comparing routine may not be so complicated. The following algorithm may be used: 1) skip same leading chars, 2) if remaining parts begin with digits, compare strings numerically 3) otherwise, compare strings literally.

Below is a sample function.
It's not perfect, but it works for the most cases.

---------------
// returns true, if x < y, false otherwise.
bool CompareNumericLess(LPCTSTR x_str, LPCTSTR y_str)
{
  // skip same characters
  while ((*x_str || *y_str) && *x_str == *y_str)
  {
    ++x_str;
    ++y_str;
  }

  // parse numeric part of the first arg
  if (IsCharAlphaNumeric(*x_str) && !IsCharAlpha(*x_str))
  {
    UINT x_num = *x_str - '0';
    LPCTSTR x_str_tmp = x_str + 1;
    while (IsCharAlphaNumeric(*x_str_tmp) && !IsCharAlpha(*x_str_tmp))
    {
      x_num = 10 * x_num + *x_str_tmp - '0';
      ++x_str_tmp;
    }

    // parse numeric part of the second arg
    if (IsCharAlphaNumeric(*y_str) && !IsCharAlpha(*y_str))
    {
      UINT y_num = *y_str - '0';
      LPCTSTR y_str_tmp = y_str + 1;
      while (IsCharAlphaNumeric(*y_str_tmp) && !IsCharAlpha(*y_str_tmp))
      {
        y_num = 10 * y_num + *y_str_tmp - '0';
        ++y_str_tmp;
      }

      // compare numeric parts of the arguments
      return x_num < y_num;
    }
  }
  // otherwise, compare literally
  return *y_str && (!*x_str || *x_str < *y_str);
}
---------------

Sincerely yours,
Ivan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tortoisesvn.tigris.org
For additional commands, e-mail: dev-help@tortoisesvn.tigris.org
Received on Tue Apr 11 10:46:48 2006

This is an archived mail posted to the TortoiseSVN Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.