How to sort file names with numbers and alphabets in order in C?
c, c++, sorting
Solution
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;
}
Notes:
- 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.
Problem
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); } } } ``` But I need to sort it as order seen in Windows explorer How to sort like this way? Please help