Sign In Register

How can we help you today?

Start a new topic
Answered

French words

Hi,

   I have several entries in french (with accents). When i try to get all these entries in alphabetical order, the names beginning with a "À" ("a" with accent) are at the end.



Best Answer

Well, I think I found something


  

allMatters.sort(function(a, b){
    var nameA = RemoveAccents(a[Object.keys(a)[0]]);
    var nameB = RemoveAccents(b[Object.keys(b)[0]]);

  if ( nameA < nameB )
       return -1;
     if ( nameA > nameB )
       return 1;
     return 0;
})


function RemoveAccents(str) {
  var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
  str = str.split('');
  var strLen = str.length;
  var i, x;
  for (i = 0; i < strLen; i++) {
    if ((x = accents.indexOf(str[i])) != -1) {
      str[i] = accentsOut[x];
    }
  }
  return str.join('');
}

   with this code, I can have the sorting that i want with the game name (in French)

Hope that code will help others


Hi Savie, 


You could try having a duplicate searchable field in the same document that doesn't have accented characters and use that field to sort your results, then when displaying them in game use the originally accented ones.


{
"myField": "À"
"mySearchableField": "A"
}

  

Let me know if that helps. I'll keep looking into this for you in the mean time.


Thanks,

Liam

Thanks. it will help

Answer

Well, I think I found something


  

allMatters.sort(function(a, b){
    var nameA = RemoveAccents(a[Object.keys(a)[0]]);
    var nameB = RemoveAccents(b[Object.keys(b)[0]]);

  if ( nameA < nameB )
       return -1;
     if ( nameA > nameB )
       return 1;
     return 0;
})


function RemoveAccents(str) {
  var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz";
  str = str.split('');
  var strLen = str.length;
  var i, x;
  for (i = 0; i < strLen; i++) {
    if ((x = accents.indexOf(str[i])) != -1) {
      str[i] = accentsOut[x];
    }
  }
  return str.join('');
}

   with this code, I can have the sorting that i want with the game name (in French)

Hope that code will help others

Login to post a comment