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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting problems

Sorting problems

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialalgorithmshelp
7 Posts 2 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.
  • R Offline
    R Offline
    Rafael Fernandez Lopez
    wrote on last edited by
    #1

    Well, my app is using CStringArray and CUIntArray, and I would like to know how to sort them, I mean, if I insert into my collection lots of times (example: "12:15", "18:25"...) I would like to know how to sort them ascendent or descendent. Same with CUIntArray. Please help, I think this is interesting... Thank you !!


    Written by: Rafael Fernández López.

    void life ( bool me, bool her ) {

    if ( her ) { me = true ; } else { me = false ; }

    }

    U 2 Replies Last reply
    0
    • R Rafael Fernandez Lopez

      Well, my app is using CStringArray and CUIntArray, and I would like to know how to sort them, I mean, if I insert into my collection lots of times (example: "12:15", "18:25"...) I would like to know how to sort them ascendent or descendent. Same with CUIntArray. Please help, I think this is interesting... Thank you !!


      Written by: Rafael Fernández López.

      void life ( bool me, bool her ) {

      if ( her ) { me = true ; } else { me = false ; }

      }

      U Offline
      U Offline
      utkarsharma
      wrote on last edited by
      #2

      good one ,not the question but the Signature...take care... utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

      R 1 Reply Last reply
      0
      • U utkarsharma

        good one ,not the question but the Signature...take care... utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

        R Offline
        R Offline
        Rafael Fernandez Lopez
        wrote on last edited by
        #3

        Yes, I'm from spain and my english is not very good. Well, do you know how to do it ?? I would like to have some kind of object/function/class that would be able to sort a collection or something like that. Thanks in advance.


        Written by: Rafael Fernández López.

        void life ( bool me, bool her ) {

        if ( her ) { me = true ; } else { me = false ; }

        }

        1 Reply Last reply
        0
        • R Rafael Fernandez Lopez

          Well, my app is using CStringArray and CUIntArray, and I would like to know how to sort them, I mean, if I insert into my collection lots of times (example: "12:15", "18:25"...) I would like to know how to sort them ascendent or descendent. Same with CUIntArray. Please help, I think this is interesting... Thank you !!


          Written by: Rafael Fernández López.

          void life ( bool me, bool her ) {

          if ( her ) { me = true ; } else { me = false ; }

          }

          U Offline
          U Offline
          utkarsharma
          wrote on last edited by
          #4

          /*Assuming we have a CStringArray strItems, then this code sorts it using a very simple bubble sort .There are many different sorting algorithms which you could use */ for (int nOuter = 0; nOuter < strItems.GetSize() - 1; nOuter++) { for (int nInner = strItems.GetSize() - 1; nInner > nOuter; --nInner) { if (strItems[nInner - 1] > strItems[nInner]) { CString strTemp = strItems[nInner]; strItems[nInner] = strItems[nInner - 1]; strItems[nInner - 1] = strTemp; }//end of if }//end of inner for }// end of outer for ----- and previous mail was not a fun of any body's english but I feel u'r signature is ossum.. utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

          R U 3 Replies Last reply
          0
          • U utkarsharma

            /*Assuming we have a CStringArray strItems, then this code sorts it using a very simple bubble sort .There are many different sorting algorithms which you could use */ for (int nOuter = 0; nOuter < strItems.GetSize() - 1; nOuter++) { for (int nInner = strItems.GetSize() - 1; nInner > nOuter; --nInner) { if (strItems[nInner - 1] > strItems[nInner]) { CString strTemp = strItems[nInner]; strItems[nInner] = strItems[nInner - 1]; strItems[nInner - 1] = strTemp; }//end of if }//end of inner for }// end of outer for ----- and previous mail was not a fun of any body's english but I feel u'r signature is ossum.. utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

            R Offline
            R Offline
            Rafael Fernandez Lopez
            wrote on last edited by
            #5

            Well, i'm going to test it, thank you very much... altough I don't underestand that code completely because I don't think it will sort them alphabetical, but if you say so, I believe you. PD: I know my english is not very good, and I'm sorry about that. Thank you for your effort. Thank you so much !!!


            Written by: Rafael Fernández López.

            void life ( bool me, bool her ) {

            if ( her ) { me = true ; } else { me = false ; }

            }

            1 Reply Last reply
            0
            • U utkarsharma

              /*Assuming we have a CStringArray strItems, then this code sorts it using a very simple bubble sort .There are many different sorting algorithms which you could use */ for (int nOuter = 0; nOuter < strItems.GetSize() - 1; nOuter++) { for (int nInner = strItems.GetSize() - 1; nInner > nOuter; --nInner) { if (strItems[nInner - 1] > strItems[nInner]) { CString strTemp = strItems[nInner]; strItems[nInner] = strItems[nInner - 1]; strItems[nInner - 1] = strTemp; }//end of if }//end of inner for }// end of outer for ----- and previous mail was not a fun of any body's english but I feel u'r signature is ossum.. utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

              U Offline
              U Offline
              utkarsharma
              wrote on last edited by
              #6

              hi honey.. You can use the sample code sort a CStringArray object. The main() function constructs a CStringArray object, adds elements to it, prints out the elements, calls the sort() member function to sort it, and then prints the sorted elements. The sort() function uses the Bubble Sort algorithm to sort the array and calls the CompareAndSwap() function to compare each string and swap them if necessary. //Sample Code /* * Compile options needed: /MT */ class CSortStringArray : public CStringArray { public: void Sort(); private: BOOL CompareAndSwap(int pos); }; void CSortStringArray::Sort() { BOOL bNotDone = TRUE; while (bNotDone) { bNotDone = FALSE; for(int pos = 0;pos < GetUpperBound();pos++) bNotDone |= CompareAndSwap(pos); } } BOOL CSortStringArray::CompareAndSwap(int pos) { CString temp; int posFirst = pos; int posNext = pos + 1; if (GetAt(posFirst).CompareNoCase(GetAt(posNext)) > 0) { temp = GetAt(posFirst); SetAt(posFirst, GetAt(posNext)); SetAt(posNext, temp); return TRUE; } return FALSE; } void main() { CSortStringArray sortArray; sortArray.Add(CString("Zebra")); sortArray.Add(CString("Bat")); sortArray.Add(CString("Apple")); sortArray.Add(CString("Mango")); for (int i = 0; i <= sortArray.GetUpperBound(); i++) cout << sortArray[i] << endl; sortArray.Sort(); cout << endl; for (int j = 0; j <= sortArray.GetUpperBound(); j++) cout << sortArray[j] << endl; } take care... :zzz::zzz: utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

              1 Reply Last reply
              0
              • U utkarsharma

                /*Assuming we have a CStringArray strItems, then this code sorts it using a very simple bubble sort .There are many different sorting algorithms which you could use */ for (int nOuter = 0; nOuter < strItems.GetSize() - 1; nOuter++) { for (int nInner = strItems.GetSize() - 1; nInner > nOuter; --nInner) { if (strItems[nInner - 1] > strItems[nInner]) { CString strTemp = strItems[nInner]; strItems[nInner] = strItems[nInner - 1]; strItems[nInner - 1] = strTemp; }//end of if }//end of inner for }// end of outer for ----- and previous mail was not a fun of any body's english but I feel u'r signature is ossum.. utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

                U Offline
                U Offline
                utkarsharma
                wrote on last edited by
                #7

                hi honey.. You can use the sample code sort a CStringArray object. The main() function constructs a CStringArray object, adds elements to it, prints out the elements, calls the sort() member function to sort it, and then prints the sorted elements. The sort() function uses the Bubble Sort algorithm to sort the array and calls the CompareAndSwap() function to compare each string and swap them if necessary. //Sample Code /* * Compile options needed: /MT */ class CSortStringArray : public CStringArray { public: void Sort(); private: BOOL CompareAndSwap(int pos); }; void CSortStringArray::Sort() { BOOL bNotDone = TRUE; while (bNotDone) { bNotDone = FALSE; for(int pos = 0;pos < GetUpperBound();pos++) bNotDone |= CompareAndSwap(pos); } } BOOL CSortStringArray::CompareAndSwap(int pos) { CString temp; int posFirst = pos; int posNext = pos + 1; if (GetAt(posFirst).CompareNoCase(GetAt(posNext)) > 0) { temp = GetAt(posFirst); SetAt(posFirst, GetAt(posNext)); SetAt(posNext, temp); return TRUE; } return FALSE; } void main() { CSortStringArray sortArray; sortArray.Add(CString("Zebra")); sortArray.Add(CString("Bat")); sortArray.Add(CString("Apple")); sortArray.Add(CString("Mango")); for (int i = 0; i <= sortArray.GetUpperBound(); i++) cout << sortArray[i] << endl; sortArray.Sort(); cout << endl; for (int j = 0; j <= sortArray.GetUpperBound(); j++) cout << sortArray[j] << endl; } take care... :zzz::zzz: utkarsh sharma "Not everything that counts can be counted, and not everything that can be counted counts."

                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