Is there any way to sort strings in all languages?

瘦欲@ 提交于 2019-12-03 02:28:32

Because of every language has its own alphabetic order you can not. For example,

Russian language as you stated has с letter has a different order than Turkish language.

You should always use collator. What I can suggest you is to us Collection API.

    //
    // Define a collator for German language
    //
    Collator collator = Collator.getInstance(Locale.GERMAN);

    //
    // Sort the list using Collator
    //
    Collections.sort(words, collator);

For futher information check and as stated here

This program shows what can happen when you sort the same list of words with two different collators:

Collator fr_FRCollator = Collator.getInstance(new Locale("fr","FR"));

Collator en_USCollator = Collator.getInstance(new Locale("en","US"));

The method for sorting, called sortStrings, can be used with any Collator. Notice that the sortStrings method invokes the compare method:

 public static void sortStrings(Collator collator, 
                           String[] words) {
  String tmp;
     for (int i = 0; i < words.length; i++) {
        for (int j = i + 1; j < words.length; j++) { 
           if (collator.compare(words[i], words[j]) > 0) {
              tmp = words[i];
              words[i] = words[j];
              words[j] = tmp;
           }
         }
      }
 }

The English Collator sorts the words as follows:

peach péché pêche sin

According to the collation rules of the French language, the preceding list is in the wrong order. In French péché should follow pêche in a sorted list. The French Collator sorts the array of words correctly, as follows:

peach pêche péché sin

Even if you could accurately detect the language being used, useful collation orders are usually specific to a particular language+country combination. And even within a language+country, collation can vary depending on usage or certain customisations.

However, if you do need to sort arbitrary sets of text, your best bet is the Unicode Collation Algorithm, which defines a language-independent collation for any Unicode text. The algorithm is customisable, but doesn't necessary give results that make sense to any one culture (and definitely not across them).

Java's collation classes don't implement this algorithm, but it is available as part of ICU's RuleBaseCollator.

As far I know, the Chinese do not have any order for their language, the Japanes possible have the order in the Hiragana or Katakana, but in Kanji it is doubtful. But in computers sience everything is represented by numbers same thing goes for languages sings. Each sign correspond to unique UNICODE number. So this might be the solution for you, sort the words using their UNICODE positions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!