Sequentially sorting
-
I have a CStringArray filled with a bunch of filenames. The filenames are like this... 1.jpg, 2.jpg, 3.jpg, and so on. I have over a thousand of these. From time to time and for some reason I need to go through and rename all of them. I use CFileFind with FindFile() and FindNextFile()to fill the string array with the filenames. But, they're sorted alphabetically, 1.jpg, 10.jpg, 100.jpg, 2.jpg, 20.jpg, etc. I need these to be sorted sequentially. I cant seem to figure out how to do it. All I need is to sort the filenames in the string array sequentially. Any help? Thanks, Daniel
-
I have a CStringArray filled with a bunch of filenames. The filenames are like this... 1.jpg, 2.jpg, 3.jpg, and so on. I have over a thousand of these. From time to time and for some reason I need to go through and rename all of them. I use CFileFind with FindFile() and FindNextFile()to fill the string array with the filenames. But, they're sorted alphabetically, 1.jpg, 10.jpg, 100.jpg, 2.jpg, 20.jpg, etc. I need these to be sorted sequentially. I cant seem to figure out how to do it. All I need is to sort the filenames in the string array sequentially. Any help? Thanks, Daniel
string sorting is different than numerical sorting. if your filenames are only .jpg then, convert the number from string to number and sort according to the numbers.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
string sorting is different than numerical sorting. if your filenames are only .jpg then, convert the number from string to number and sort according to the numbers.
Maximilien Lincourt Your Head A Splode - Strong Bad
I tried that first. Sorting an array of ints isnt any easier. Know of an easy way to sequentially sort an array of ints?
-
I tried that first. Sorting an array of ints isnt any easier. Know of an easy way to sequentially sort an array of ints?
have a look at QuickSort enabled CArray template class
Maximilien Lincourt Your Head A Splode - Strong Bad
-
have a look at QuickSort enabled CArray template class
Maximilien Lincourt Your Head A Splode - Strong Bad
I figured this out...
for (int yy = 0; yy < Size; yy++) { for (int y = 1; y < Size; y++) { if (c[y] < c[y-1]) { a = c[y-1]; b = c[y]; c[y] = a; c[y-1] = b; a = b = 0; } } }
Size = size of int array. Its ugly, but it works. -
I figured this out...
for (int yy = 0; yy < Size; yy++) { for (int y = 1; y < Size; y++) { if (c[y] < c[y-1]) { a = c[y-1]; b = c[y]; c[y] = a; c[y-1] = b; a = b = 0; } } }
Size = size of int array. Its ugly, but it works.Daniel1324 wrote: for (int yy = 0; yy < Size; yy++) { for (int y = 1; y < Size; y++) { if (c[y] < c[y-1]) { a = c[y-1]; b = c[y]; c[y] = a; c[y-1] = b; a = b = 0; } } } Size = size of int array. :eek: Yikes! Have a look at the
qsort()
C library function. You should be able to do this:static int intCompareFunc(const void *parm1, const void *parm2)
{
int num1 = *static_cast<const int*>(parm1);
int num2 = *static_cast<const int*>(parm2);
return num1 - num2;
}
...
qsort(c, sizeof(int), Size, intCompareFunc);That should sort the
c
array in ascending order.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
I figured this out...
for (int yy = 0; yy < Size; yy++) { for (int y = 1; y < Size; y++) { if (c[y] < c[y-1]) { a = c[y-1]; b = c[y]; c[y] = a; c[y-1] = b; a = b = 0; } } }
Size = size of int array. Its ugly, but it works.The best way to do these kinds of operation these days is to use STL. See my article here: http://www.codeproject.com/vcpp/stl/legacytostl.asp[^] You can apply STL algorithms to ordinary arrays and the MFC CArray as well as to vector. Kevin