aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/intl/collator/index.html
blob: e926a56ba5f331591feaa1382cdd4f031f25dfa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
---
title: Intl.Collator
slug: Web/JavaScript/Reference/Global_Objects/Intl/Collator
tags:
  - Class
  - Collator
  - Internationalization
  - Intl
  - JavaScript
  - Localization
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/Collator
---
<div>{{JSRef}}</div>

<p><strong><code>Intl.Collator</code></strong> オブジェクトは、言語を考慮した文字列の比較を可能にします。</p>

<div>{{EmbedInteractiveExample("pages/js/intl-collator.html")}}</div>
<!--このデモのソースファイルは GitHub リポジトリーに格納されています。デモプロジェクトに協力していただける場合は、 https://github.com/mdn/interactive-examples をクローンしてプルリクエストを送信してください。 -->

<h2 id="Constructor">コンストラクター</h2>

<dl>
	<dt>{{jsxref("Intl/Collator/Collator", "Intl.Collator()")}}</dt>
	<dd>新しい <code>Collator</code> オブジェクトを生成します。</dd>
</dl>

<h2 id="Static_methods">静的メソッド</h2>

<dl>
	<dt>{{jsxref("Intl/Collator/supportedLocalesOf", "Intl.Collator.supportedLocalesOf()")}}</dt>
	<dd>指定したロケールのうち、ランタイムの既定のロケールに代替されることなく対応されているものを含む配列を返します。</dd>
</dl>

<h2 id="Instance_methods">インスタンスメソッド</h2>

<dl>
	<dt>{{jsxref("Intl/Collator/compare", "Intl.Collator.prototype.compare")}}</dt>
	<dd>この {{jsxref("Global_Objects/Intl/Collator", "Intl.Collator")}} オブジェクトのソート順に応じて 2 つの文字列を比較する関数を返します。</dd>
	<dt>{{jsxref("Intl/Collator/resolvedOptions", "Intl.Collator.prototype.resolvedOptions()")}}</dt>
	<dd>ローケルを反映しているプロパティとオブジェクトの初期化中に計算された照合オプションをもった新しいオブジェクトを返します。</dd>
</dl>

<h2 id="Examples"></h2>

<h3 id="Using_Collator">Collator の使用</h3>

<p>次の例では、一方の文字列が他方の文字列の前である場合、後である場合、および同じレベルである場合の比較結果を表示します。</p>

<pre class="brush: js notranslate">console.log(new Intl.Collator().compare('a', 'c')); // → 負の値
console.log(new Intl.Collator().compare('c', 'a')); // → 正の値
console.log(new Intl.Collator().compare('a', 'a')); // → 0
</pre>

<p>上記コードの結果は、ブラウザーやブラウザーのバージョンによって異なる可能性がある点に注意してください。というのも前および後を表す数値については、それぞれ負の数および正の数であることだけが仕様によって定めています。具体的な数値は定められていないため、実装ごとに異なる数値になるかもしれません。</p>

<h3 id="Using_locales">locales の使用</h3>

<p>{{jsxref("Intl/Collator/compare")}} の結果は言語によって異なります。アプリケーションのユーザーインターフェイスで使用される言語のソート順を取得するには、 <code><var>locales</var></code> 引数にその言語を (およびフォールバック用の言語も) 指定してください。</p>

<pre class="brush: js">// in German, ä sorts with a
console.log(new Intl.Collator('de').compare('ä', 'z'));
// → 負の値

// in Swedish, ä sorts after z
console.log(new Intl.Collator('sv').compare('ä', 'z'));
// → 正の値
</pre>

<h3 id="Using_options">options の使用</h3>

<p>T{{jsxref("Intl/Collator/compare")}} の結果は <code><var>options</var></code> 引数でカスタマイズできます。</p>

<pre class="brush: js">// in German, ä has a as the base letter
console.log(new Intl.Collator('de', { sensitivity: 'base' }).compare('ä', 'a'));
// → 0

// in Swedish, ä and a are separate base letters
console.log(new Intl.Collator('sv', { sensitivity: 'base' }).compare('ä', 'a'));
// → 正の値
</pre>

<h2 id="Specifications">仕様書</h2>

<table class="standard-table">
	<thead>
		<tr>
			<th scope="col">仕様書</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>{{SpecName('ES Int Draft', '#collator-objects', 'Intl.Collator')}}</td>
		</tr>
	</tbody>
</table>

<h2 id="Browser_compatibility">ブラウザーの互換性</h2>

<div>{{Compat("javascript.builtins.Intl.Collator")}}</div>

<h2 id="See_also">関連情報</h2>

<ul>
	<li>{{jsxref("Intl")}}</li>
</ul>