如何用数字和字母的文件名,以便使用C排序? [英] How to sort file names with numbers and alphabets in order in C?

查看:221
本文介绍了如何用数字和字母的文件名,以便使用C排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code到的字母顺序排序文件并显示在图中排序文件:

I have used the following code to sort files in alphabetical order and it sorts the files as shown in the figure:

for(int i = 0;i < maxcnt;i++) 
{
    for(int j = i+1;j < maxcnt;j++)
    {           
        if(strcmp(Array[i],Array[j]) > 0)
        {            
            strcpy(temp,Array[i]);      
            strcpy(Array[i],Array[j]);      
            strcpy(Array[j],temp);    
        }    
    } 
}

但我需要对它进行排序的顺序在Windows资源管理器看到

But I need to sort it as order seen in Windows explorer

如何排序这样的方式?请帮助

How to sort like this way? Please help

推荐答案

对于 C 的答案,以下是 strcasecmp()的替代。此功能递归处理包含交替的数字和非数字子串。你可以用的qsort使用()

For a C answer, the following is a replacement for strcasecmp(). This function recurses to handle strings that contain alternating numeric and non-numeric substrings. You can use it with qsort():

int strcasecmp_withNumbers(const void *void_a, const void *void_b) {
   const char *a = void_a;
   const char *b = void_b;

   if (!a || !b) { // if one doesn't exist, other wins by default
      return a ? 1 : b ? -1 : 0;
   }
   if (isdigit(*a) && isdigit(*b)) { // if both start with numbers
      char *remainderA;
      char *remainderB;
      long valA = strtol(a, &remainderA, 10);
      long valB = strtol(b, &remainderB, 10);
      if (valA != valB)
         return valA - valB;
      // if you wish 7 == 007, comment out the next two lines
      else if (remainderB - b != remainderA - a) // equal with diff lengths
         return (remainderB - b) - (remainderA - a); // set 007 before 7
      else // if numerical parts equal, recurse
         return strcasecmp_withNumbers(remainderA, remainderB);
   }
   if (isdigit(*a) || isdigit(*b)) { // if just one is a number
      return isdigit(*a) ? -1 : 1; // numbers always come first
   }
   while (*a && *b) { // non-numeric characters
      if (isdigit(*a) || isdigit(*b))
         return strcasecmp_withNumbers(a, b); // recurse
      if (tolower(*a) != tolower(*b))
         return tolower(*a) - tolower(*b);
      a++;
      b++;
   }
   return *a ? 1 : *b ? -1 : 0;
}

注:


  • Windows需要 stricmp(),而不是Unix的等价 strcasecmp()

  • 上面code会(显然)给出不正确的结果,如果数字的真正的大。

  • 前导零这里忽略。在我区,这是一个特性,而不是一个错误:我们平时想UAL0123匹配UAL123。但是,这可能是也可能不是你所需要的。

  • 参见排序上可能包含一些和的如何实现在C的自然排序算法++?,虽然有答案,或者在他们的联系,当然长,东拉西扯上述code相比,约中的至少四倍。

  • Windows needs stricmp() rather than the Unix equivalent strcasecmp().
  • The above code will (obviously) give incorrect results if the numbers are really big.
  • Leading zeros are ignored here. In my area, this is a feature, not a bug: we usually want UAL0123 to match UAL123. But this may or may not be what you require.
  • See also Sort on a string that may contain a number and How to implement a natural sort algorithm in c++?, although the answers there, or in their links, are certainly long and rambling compared with the above code, by about a factor of at least four.

这篇关于如何用数字和字母的文件名,以便使用C排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆