Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sequentially sorting

Sequentially sorting

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structureshelptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel1324
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • D Daniel1324

      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

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • M Maximilien

        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

        D Offline
        D Offline
        Daniel1324
        wrote on last edited by
        #3

        I tried that first. Sorting an array of ints isnt any easier. Know of an easy way to sequentially sort an array of ints?

        M 1 Reply Last reply
        0
        • D Daniel1324

          I tried that first. Sorting an array of ints isnt any easier. Know of an easy way to sequentially sort an array of ints?

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          have a look at QuickSort enabled CArray template class


          Maximilien Lincourt Your Head A Splode - Strong Bad

          D 1 Reply Last reply
          0
          • M Maximilien

            have a look at QuickSort enabled CArray template class


            Maximilien Lincourt Your Head A Splode - Strong Bad

            D Offline
            D Offline
            Daniel1324
            wrote on last edited by
            #5

            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.

            R K 2 Replies Last reply
            0
            • D Daniel1324

              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.

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              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"

              1 Reply Last reply
              0
              • D Daniel1324

                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.

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups