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. sotring/comparing strings

sotring/comparing strings

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsquestion
6 Posts 3 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.
  • J Offline
    J Offline
    josip cagalj
    wrote on last edited by
    #1

    Hi to all. I'm trying to sort strings which contains some eastern European specific letters. I'm sorting with qsort function: qsort( (void *)polje, (size_t)cnt, sizeof( char * ), compare ); where 'compare' is a helper function int compare( const void *arg1, const void *arg2 ) which uses '_stricoll' function. Wen running my test app sorting doesn't work. I then tried to set local settings like this on OnInitDialog():

    char\* locale;
    locale = setlocale(LC\_ALL,"Croatian");
    

    locale returns "Croatian_Croatia.1250"; but sorting still isn't working, why? Thanks in advance

    S D 2 Replies Last reply
    0
    • J josip cagalj

      Hi to all. I'm trying to sort strings which contains some eastern European specific letters. I'm sorting with qsort function: qsort( (void *)polje, (size_t)cnt, sizeof( char * ), compare ); where 'compare' is a helper function int compare( const void *arg1, const void *arg2 ) which uses '_stricoll' function. Wen running my test app sorting doesn't work. I then tried to set local settings like this on OnInitDialog():

      char\* locale;
      locale = setlocale(LC\_ALL,"Croatian");
      

      locale returns "Croatian_Croatia.1250"; but sorting still isn't working, why? Thanks in advance

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      All I can suggest is splitting the problem into smaller parts. First, check that _stricoll (and thus your compare function) compare strings as you would expect. After that...well, can you confirm that polje is of type char* polje[], i.e. an array of character pointers?

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      J 1 Reply Last reply
      0
      • S Stuart Dootson

        All I can suggest is splitting the problem into smaller parts. First, check that _stricoll (and thus your compare function) compare strings as you would expect. After that...well, can you confirm that polje is of type char* polje[], i.e. an array of character pointers?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        J Offline
        J Offline
        josip cagalj
        wrote on last edited by
        #3

        Thanks for replay, code:

        CString \*polje = new CString\[cnt\];
        for(int i=0; i<cnt;i++)	{
        	CString pom;
        	m\_List.GetText(i,pom);
        	polje\[i\] = pom;
        }
        qsort( (void \*)polje, (size\_t)cnt, sizeof( char \* ), compare );
        

        I have a list box control where user can add strings for sorting. Posted code is extracted from OnOK()!

        S 1 Reply Last reply
        0
        • J josip cagalj

          Thanks for replay, code:

          CString \*polje = new CString\[cnt\];
          for(int i=0; i<cnt;i++)	{
          	CString pom;
          	m\_List.GetText(i,pom);
          	polje\[i\] = pom;
          }
          qsort( (void \*)polje, (size\_t)cnt, sizeof( char \* ), compare );
          

          I have a list box control where user can add strings for sorting. Posted code is extracted from OnOK()!

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Right...well, that sizeof(char*) in the qsort call isn't going to be correct, is it? I would suggest:

          1. Change compare so it expects its two parameters to be pointers to CString
          2. Change the sizeof( char* ) to be sizeof( CString )

          And here's a thought - if you used STL containers, you could use type-safe algorithms and things that would highlight these issues...

          std::vector polje(cnt);
          for(int i=0; i<cnt;++cnt)
          {
          CString pom;
          m_List.GetText(i,pom);
          polje.push_back(pom);
          }
          std::sort(polje.begin(), polje.end(), compareFunction);

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • J josip cagalj

            Hi to all. I'm trying to sort strings which contains some eastern European specific letters. I'm sorting with qsort function: qsort( (void *)polje, (size_t)cnt, sizeof( char * ), compare ); where 'compare' is a helper function int compare( const void *arg1, const void *arg2 ) which uses '_stricoll' function. Wen running my test app sorting doesn't work. I then tried to set local settings like this on OnInitDialog():

            char\* locale;
            locale = setlocale(LC\_ALL,"Croatian");
            

            locale returns "Croatian_Croatia.1250"; but sorting still isn't working, why? Thanks in advance

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            josip cagalj wrote:

            Wen running my test app sorting doesn't work.

            For anything or just eastern European specific letters.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            J 1 Reply Last reply
            0
            • D David Crow

              josip cagalj wrote:

              Wen running my test app sorting doesn't work.

              For anything or just eastern European specific letters.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              J Offline
              J Offline
              josip cagalj
              wrote on last edited by
              #6

              Just eastern European letters.

              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