Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3304364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:03:16+00:00 2026-05-17T21:03:16+00:00

The problem I’m having is that I need to sort a whole bunch of

  • 0

The problem I’m having is that I need to sort a whole bunch of char pointers, but they have special characters. I managed to get a sorting procedure like so:

std::sort(dict_.begin(), dict_.end(), comp);

bool comp(NumPair& a, NumPair& b)
{
    return boost::lexicographic_compare(a.pFirst, b.pFirst);
}

This worked great, except that all special german characters were sorted before all the others. My teacher (yes, this is pertaining to a homework assignment), however, wants them to be sorted at the end. Awesome!

So I was playing around and thought I could use a trick I saw on a website to enable a regional locale to include the special characters like so

return boost::lexicographic_compare(a.pFirst, b.pFirst, locale("german"));

Didn’t work! So:

bool comp()
{
    setlocale(LC_ALL, "");
    return boost::lexicographic_compare(a.pFirst, b.pFirst);
}

Didn’t work!

If you have them, I would love to hear some other ideas that might actually work.

Update:

As requested, some sample input and output:

// Some entries
dict_.push_back( NumPair ( "öffnen", "to open" ) );
dict_.push_back( NumPair ( "überraschen", "to surprise" ) );
dict_.push_back( NumPair ( "wünschen", "to wish, to desire, to want" ) );
dict_.push_back( NumPair ( "widersprechen", "to contradict_" ) );

// NumPair ctor.
NumPair( const char *pFirst, const char *pSecond )
{
    /* Deep copy of pFirst and pSecond */
}

Output after result:

öffnen
überraschen
wünschen
widersprechen
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:03:16+00:00Added an answer on May 17, 2026 at 9:03 pm

    You might want to show more of your code, like exactly what strings you’re using that are causing this problem. I’m easily able to sort a set of German words, and any words beginning with non-ASCII special German characters are ordered at the end. This happens even without any special German locale settings, since in Unicode non-ASCII characters have higher codepoint values than ASCII characters.

    For example:

    setlocale(LC_ALL, "");
    
    std::vector<std::wstring> vec;
    vec.push_back(L"Hallo");
    vec.push_back(L"Morgen");
    vec.push_back(L"Zebra");
    vec.push_back(L"Abend");
    vec.push_back(L"Übertragens");
    vec.push_back(L"Buchen");
    
    std::sort(vec.begin(), vec.end());
    for (std::vector<std::wstring>::iterator it = vec.begin(); it != vec.end(); ++it)
        std::wcout << *it << std::endl;
    

    This outputs:

    Abend
    Buchen
    Hallo
    Morgen
    Zebra
    Übertragens
    

    Note the use of wide character strings. Since lexicographical comparison routines compare character-by-character, you need to use wide characters or else the comparison function will end up comparing the string byte-by-byte instead of character-by-character. This will result in invalid comparisons since not every Unicode character can be stored in a single byte. Special German characters, for example, are 2 bytes in UTF-8, so you need a data type capable of containing the range of 0x00 to 0xFFFF in a single element. On most platforms, wchar_t is sufficient for this.

    (Also note that it’s not a good practice to include non-ASCII characters in source code. Use “universal character codes” instead. I’m just using non-ASCII source here for clarity.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem I have timestamped data, which I need to search based on the timestamp
Problem: We have a web app that calls some web services asynchronously (from the
I have a French site that I want to parse, but am running into
Problem: I have an address field from an Access database which has been converted
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: Ajax suggest-search on [ n ] ingredients in recipes. That is: match recipes
Problem solved: Thanks guys, see my answer below. I have a website running in
Problem I want to validate a field using a custom validator I've created that's
Problem: We have to read from a proprietary binary file at work. It changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.