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

  • Home
  • SEARCH
  • 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 89663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:45:06+00:00 2026-05-10T22:45:06+00:00

For a poor man’s implementation of near -collation-correct sorting on the client side I

  • 0

For a poor man’s implementation of near-collation-correct sorting on the client side I need a JavaScript function that does efficient single character replacement in a string.

Here is what I mean (note that this applies to German text, other languages sort differently):

 native sorting gets it wrong: a b c o u z ä ö ü collation-correct would be:   a ä b c o ö u ü z 

Basically, I need all occurrences of "ä" of a given string replaced with "a" (and so on). This way the result of native sorting would be very close to what a user would expect (or what a database would return).

Other languages have facilities to do just that: Python supplies str.translate(), in Perl there is tr/…/…/, XPath has a function translate(), ColdFusion has ReplaceList(). But what about JavaScript?

Here is what I have right now.

// s would be a rather short string (something like  // 200 characters at max, most of the time much less) function makeSortString(s) {   var translate = {     "ä": "a", "ö": "o", "ü": "u",     "Ä": "A", "Ö": "O", "Ü": "U"   // probably more to come   };   var translate_re = /[öäüÖÄÜ]/g;   return ( s.replace(translate_re, function(match) {      return translate[match];    }) ); } 

For starters, I don’t like the fact that the regex is rebuilt every time I call the function. I guess a closure can help in this regard, but I don’t seem to get the hang of it for some reason.

Can someone think of something more efficient?


Answers below fall in two categories:

  1. String replacement functions of varying degrees of completeness and efficiency (what I was originally asking about)
  2. A late mention of String#localeCompare, which is now widely supported among JS engines (not so much at the time of the question) and could solve this category of problem much more elegantly.
  • 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. 2026-05-10T22:45:07+00:00Added an answer on May 10, 2026 at 10:45 pm

    I can’t speak to what you are trying to do specifically with the function itself, but if you don’t like the regex being built every time, here are two solutions and some caveats about each.

    Here is one way to do this:

    function makeSortString(s) {   if(!makeSortString.translate_re) makeSortString.translate_re = /[öäüÖÄÜ]/g;   var translate = {     'ä': 'a', 'ö': 'o', 'ü': 'u',     'Ä': 'A', 'Ö': 'O', 'Ü': 'U'   // probably more to come   };   return ( s.replace(makeSortString.translate_re, function(match) {      return translate[match];    }) ); } 

    This will obviously make the regex a property of the function itself. The only thing you may not like about this (or you may, I guess it depends) is that the regex can now be modified outside of the function’s body. So, someone could do this to modify the interally-used regex:

    makeSortString.translate_re = /[a-z]/g; 

    So, there is that option.

    One way to get a closure, and thus prevent someone from modifying the regex, would be to define this as an anonymous function assignment like this:

    var makeSortString = (function() {   var translate_re = /[öäüÖÄÜ]/g;   return function(s) {     var translate = {       'ä': 'a', 'ö': 'o', 'ü': 'u',       'Ä': 'A', 'Ö': 'O', 'Ü': 'U'   // probably more to come     };     return ( s.replace(translate_re, function(match) {        return translate[match];      }) );   } })(); 

    Hopefully this is useful to you.


    UPDATE: It’s early and I don’t know why I didn’t see the obvious before, but it might also be useful to put you translate object in a closure as well:

    var makeSortString = (function() {   var translate_re = /[öäüÖÄÜ]/g;   var translate = {     'ä': 'a', 'ö': 'o', 'ü': 'u',     'Ä': 'A', 'Ö': 'O', 'Ü': 'U'   // probably more to come   };   return function(s) {     return ( s.replace(translate_re, function(match) {        return translate[match];      }) );   } })(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The document does not have an onload / load event,… May 11, 2026 at 7:30 am
  • added an answer I think it will work to put them in a… May 11, 2026 at 7:30 am
  • added an answer I'm using 2 for the scale since you said this… May 11, 2026 at 7:30 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.