日语的sort() [英] sort() for Japanese

查看:148
本文介绍了日语的sort()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将当前语言环境设置为日语,该如何使日语字符始终比非日语字符具有更高的优先级.例如,现在英文字符将始终出现在片假名字符之前.我该如何扭转这种影响?

很抱歉,不清楚.如您所见,解决方案

使用 usort ()而不是 sort(),因此您可以定义以自己的方式比较条件.

尝试这种简单的方法.我已经使用此处,并且可以正常工作.

  function mccompare($a, $b) {
    $fca = ord(substr($a, 0, 1)); $fcb = ord(substr($b, 0, 1));
    if (($fca >= 127 && $fcb >= 127) || ($fca < 127 && $fcb < 127))
      $res = $a > $b ? 1 : -1; 
    else 
      $res = $a > $b ? -1 : 1;
    return $res;
    }

  usort ($your_array, "mccompare");

所以在这个例子中

  setlocale(LC_COLLATE, "jpn");

  $your_array = array ("システム", "画面", "Windows ファイウォール",
      "インターネット オプション",  "キーボード", "メール", "音声認識", "管理ツール",
      "自動更新", "日付と時刻", "タスク", "プログラムの追加と削除", "フォント",
      "電源オプション", "マウス", "地域と言語オプション", "電話とモデムのオプション",
      "Java", "NVIDIA");

  usort ($your_array, "mccompare");
  print_r($your_array);

它返回类似的数组

Array
(
    [0] => インターネット オプション
    [1] => キーボード
    [2] => システム
    [3] => タスク
    [4] => フォント
    [5] => プログラムの追加と削除
    [6] => マウス
    [7] => メール
    [8] => 地域と言語オプション
    [9] => 日付と時刻
    [10] => 画面
    [11] => 管理ツール
    [12] => 自動更新
    [13] => 電源オプション
    [14] => 電話とモデムのオプション
    [15] => 音声認識
    [16] => Java
    [17] => NVIDIA
    [18] => Windows ファイウォール
)

注意:这只是我针对此问题的快速解决方案,并不是一个完美的解决方案.它基于检查字符串比较中的第一个字节,但是您始终可以付出一些努力,并改进此功能以针对Unicode检查所有多字节字符,然后确定$ a< = $ b或$ a> $ b. /p>

希望它对您有用!

If I have set my current locale to Japanese, how can I make it so that Japanese characters will always have higher preference than non-Japanese characters. For example, right now English characters will always appear before the Katakana characters. How can I reverse this effect?

Sorry for not being very clear. As you can see here.

The final results have Java, NVIDIA and Windows ファイアウォール. Ranked as the first three ahead of the Japanese characters. Is it possible to have those at the end?

解决方案

Use usort() instead of sort() so you can define comparing criteria at your own way.

Try this simple method. I have tried it with example from here, and it works.

  function mccompare($a, $b) {
    $fca = ord(substr($a, 0, 1)); $fcb = ord(substr($b, 0, 1));
    if (($fca >= 127 && $fcb >= 127) || ($fca < 127 && $fcb < 127))
      $res = $a > $b ? 1 : -1; 
    else 
      $res = $a > $b ? -1 : 1;
    return $res;
    }

  usort ($your_array, "mccompare");

So for this example

  setlocale(LC_COLLATE, "jpn");

  $your_array = array ("システム", "画面", "Windows ファイウォール",
      "インターネット オプション",  "キーボード", "メール", "音声認識", "管理ツール",
      "自動更新", "日付と時刻", "タスク", "プログラムの追加と削除", "フォント",
      "電源オプション", "マウス", "地域と言語オプション", "電話とモデムのオプション",
      "Java", "NVIDIA");

  usort ($your_array, "mccompare");
  print_r($your_array);

it returns array like

Array
(
    [0] => インターネット オプション
    [1] => キーボード
    [2] => システム
    [3] => タスク
    [4] => フォント
    [5] => プログラムの追加と削除
    [6] => マウス
    [7] => メール
    [8] => 地域と言語オプション
    [9] => 日付と時刻
    [10] => 画面
    [11] => 管理ツール
    [12] => 自動更新
    [13] => 電源オプション
    [14] => 電話とモデムのオプション
    [15] => 音声認識
    [16] => Java
    [17] => NVIDIA
    [18] => Windows ファイウォール
)

Note: This is just my quick solution for this problem, and it's not a perfect solution. It's based on checking first byte in comparing strings, but you can always push some effort in it and improve this function to check all multi-byte characters against Unicode and then decide if $a<=$b or $a>$b.

Hope it works for you!

这篇关于日语的sort()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆