aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/intl/collator/compare/index.html
blob: ae88bd14a4f7726b8f1f9732dda832105b95026a (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
---
title: Intl.Collator.prototype.compare
slug: Web/JavaScript/Reference/Global_Objects/Intl/Collator/compare
tags:
  - Collator
  - Internationalization
  - Intl
  - JavaScript
  - Method
  - Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/Collator/compare
---
<div>{{JSRef}}</div>

<p><strong><code>Intl.Collator.prototype.compare()</code></strong> メソッドは、2つの文字列をこの {{jsxref("Collator")}} オブジェクトのソート順に従って比較します。</p>

<div>{{EmbedInteractiveExample("pages/js/intl-collator-prototype-compare.html")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力していただける場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate"><var>collator</var>.compare(<var>string1</var>, <var>string2</var>)</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>string1</var></code></dt>
 <dt><code><var>string2</var></code></dt>
 <dd>互いに比較する文字列です。</dd>
</dl>

<h2 id="Description" name="Description">解説</h2>

<p><code>compare</code> ゲッター関数は、 <code>string1</code><code>string2</code> をこの {{jsxref("Collator")}} オブジェクトのソート順に従って比較した結果を数値で返します。 <code>string1</code><code>string2</code> の前にくる場合は負の値、 <code>string1</code><code>string2</code> の後にくる場合は正の値、等しいとみなされる場合は 0 を返します。</p>

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

<h3 id="Using_compare_for_array_sort" name="Using_compare_for_array_sort">配列の並べ替えにおける compare の使用</h3>

<p>配列の並べ替えのために <code>compare</code> ゲッター関数を使用します。なお、この関数は、取得元の collator にバインドされているので、直接 {{jsxref("Array.prototype.sort()")}} に渡すことができます。</p>

<pre class="brush: js notranslate">var a = ['Offenbach', 'Österreich', 'Odenwald'];
var collator = new Intl.Collator('de-u-co-phonebk');
a.sort(collator.compare);
console.log(a.join(', '));
// → "Odenwald, Österreich, Offenbach"
</pre>

<h3 id="Using_compare_for_array_search" name="Using_compare_for_array_search">配列の検索における compare の使用</h3>

<p>配列内の文字列の検索のために <code>compare</code> ゲッター関数を使用します。</p>

<pre class="brush: js notranslate">var a = ['Congrès', 'congres', 'Assemblée', 'poisson'];
var collator = new Intl.Collator('fr', { usage: 'search', sensitivity: 'base' });
var s = 'congres';
var matches = a.filter(v =&gt; collator.compare(v, s) === 0);
console.log(matches.join(', '));
// → "Congrès, congres"
</pre>

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

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

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

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

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

<ul>
 <li>{{jsxref("Collator", "Intl.Collator")}}</li>
 <li>{{jsxref("String.prototype.localeCompare()")}}</li>
</ul>