質問

私はパスワードというに基づく分野 attr 型文字列です。を使ってみました -

list.sort(function (a, b) {
    return a.attr - b.attr
})

が発見されたた - が表示されていない作文字列をJavaScriptです。したいので並べ替えリストに基づく人物属性型ん。

役に立ちましたか?

解決

使用 String.prototype.localeCompare ただ例

list.sort(function (a, b) {
    return ('' + a.attr).localeCompare(b.attr);
})

私の力。attrする文字列を避けることができます。 localeCompare 支持されてい Internet Explorer6以降 Firefox1.また、以下のコードを使用しないが尊重されるロケール:

if (item1.attr < item2.attr)
  return -1;
if ( item1.attr > item2.attr)
  return 1;
return 0;

他のヒント

最新の回答(2014)

私辺のこの文字列の自然ソート順のであり,この問題です。この助けになっていますよ。

長い話を短くすれば

localeCompare() サポートでは注文で使用します。指摘しているように Shog9, の答えは:

return item1.attr.localeCompare(item2.attr);

バグを見つすべてのカスタムjavascript"自然文字列の並び順"の実装

あのバカスタム実装がい文字列比較により正確にという"自然の文字列の並べ替え"

時に"遊ぶ"これらの実装についた気が不思議な"自然のソート順序高層複合ビル"あべのハルカス"、または失敗為の場合).

通常、特殊文字(スペース、ダッシュ,アンパサンド、ブラケットなど)処理されません。

ま、そして現れる混ざり合い、一般的にできる:

  • その間に大文字-'Z'および、小文字の'a'
  • その間、'9'の大文字'A'
  • その後、小文字の'z'

一見特別な文字はすべて、"グループ化"所属のメンバーが集まることで、外の空間特殊文字をもとに、常に最初の文字)を指定する。つまり、すべての種類通常は送信で、すべての間の数字と文字文字または文字小文字&大文字と"一緒に"う)は、後します。

私の結論は、そのすべての失敗を提供で一貫した受注が始追加え珍しい文字が打ち出されている。-キャラクタdiacriticsはcharctersなどのダッシュ,"マークの付います。

研究の実装:

ブラウザの"自然文字列の並び順"の実装によ localeCompare()

localeCompare() 最古の実施なしのロケールとオプション引数でサポートするIE6+、 http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs94).aspx (スクロールして、localeCompare()法です。内蔵 localeCompare() メソッドはりの仕事を選別でも、国際特別な文字です。唯一の問題の localeCompare() 方法 "ロケールに、並べ替えを使用は、完全に実装依存".する場合には、その利用localeCompareなどstringOne.localeCompare(stringTwo):Firefox、Safari、google Chrome&IEって異なる順に並べた文字列です。

研究のブラウザ-ネイティブ実装:

の難しさ"の文字列の自然ソート順序"

実装固体アルゴリズムを意味する:一貫したもの広範囲の文字は非常に難しい課題です。UTF8を含む 約2000点以上の文字 & を対象としておりこれまで120のスクリプト(言語).最後に、一部仕様がこの課題になっていない"Unicode照合アルゴリズム"することができたので http://www.unicode.org/reports/tr10/ .また、この情報についてこの質問に思 https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order

最終結論

では現在のレベルのサポートの提供するjavascriptのカスタム実装で出会ったいないでしょう何も見まく切り支援すべてこの文字-文字ます。とされているのではないかと思い利用のブラウザの種localeCompare()メソッドがあります。はい、ありの下振れのbeeing不全体のブラウザが基本試験ではもはるかに広い範囲の文字を固&意味のある並べ替えます。

でも指摘しているように Shog9, の答えは:

return item1.attr.localeCompare(item2.attr);

さらに読む:

コShog9のは嬉しい答えを導くきっかけとなった私は、"right"の方向だと思い

回答(現代ECMAScript)

list.sort((a, b) => (a.attr > b.attr) - (a.attr < b.attr))

または

list.sort((a, b) => +(a.attr > b.attr) || -(a.attr < b.attr))

説明

鋳造booleanの値を数利回りの下

  • true -> 1
  • false -> 0

を検討する上でのパターン:

  • x以y: (x > y) - (y < x) -> 1 - 0 -> 1
  • xがy: (x > y) - (y < x) -> 0 - 0 -> 0
  • xはy: (x > y) - (y < x) -> 0 - 1 -> -1

代替

  • x以y: +(x > y) || -(x < y) -> 1 || 0 -> 1
  • xがy: +(x > y) || -(x < y) -> 0 || 0 -> 0
  • xはy: +(x > y) || -(x < y) -> 0 || -1 -> -1

それらのロジックと同等の代表的なソートコンパレータ機能

if (x == y) {
    return 0;
}
return x > y ? 1 : -1;

利用するべきであ>もしくは < や==こちらです。を解決することはできない。

list.sort(function(item1, item2) {
    var val1 = item1.attr,
        val2 = item2.attr;
    if (val1 == val2) return 0;
    if (val1 > val2) return 1;
    if (val1 < val2) return -1;
});

って気にこの長いたてていただける、この冗長した理由はなぜものです。

から スペック:

Section 11.9.4   The Strict Equals Operator ( === )

The production EqualityExpression : EqualityExpression === RelationalExpression
is evaluated as follows: 
- Let lref be the result of evaluating EqualityExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating RelationalExpression.
- Let rval be GetValue(rref).
- Return the result of performing the strict equality comparison 
  rval === lval. (See 11.9.6)

そこで、私たちは今、行く11.9.6

11.9.6   The Strict Equality Comparison Algorithm

The comparison x === y, where x and y are values, produces true or false. 
Such a comparison is performed as follows: 
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
...
- If Type(x) is String, then return true if x and y are exactly the 
  same sequence of characters (same length and same characters in 
  corresponding positions); otherwise, return false.

それだけです。 トリプルequalsオペレーター用の文字列を返しますtrueを引数と同じ文字列を同一の長さと同じ文字に対応する位置).

なので === 仕事のた場合またはしようとしているこ比較文字列が来ることになっても、いずれは同じ価値観を共通に十分なシナリオのためのインライン文字列を当社のコードです。例えば、いという名前の変数 connection_state, しているかの状態に ['connecting', 'connected', 'disconnecting', 'disconnected'] ていただけるとありがたいで今、我々は直接利用できるの ===.

があります。上11.9.4が短い※

NOTE 4     
  Comparison of Strings uses a simple equality test on sequences of code 
  unit values. There is no attempt to use the more complex, semantically oriented
  definitions of character or string equality and collating order defined in the 
  Unicode specification. Therefore Strings values that are canonically equal
  according to the Unicode standard could test as unequal. In effect this 
  algorithm assumes that both Strings are already in normalized form.

うーん。何を?外部から得られた文字列のできる可能性であるが、きっunicodey、優しい === ない司法に付属 localeCompare の救済:

15.5.4.9   String.prototype.localeCompare (that)
    ...
    The actual return values are implementation-defined to permit implementers 
    to encode additional information in the value, but the function is required 
    to define a total ordering on all Strings and to return 0 when comparing
    Strings that are considered canonically equivalent by the Unicode standard. 

までに帰ります。

tl;dr;

比較文字列をjavascriptを使用 localeCompare;まっている場合、文字列にはない非アスキー部品では、例えば、内部プログラム定数、 === ものです。

入れ子複矢印機能

(a,b) => (a < b ? -1 : a > b ? 1 : 0)

お操作の最初の質問です以下の操作:

item1.attr - item2.attr

なので、仮に人数(item1.attr="1"以外の項目※2.attr="2")まだに"==="オペレーター(または他の厳格な評価者に提供することを確実にするためのタイプです。は:

return parseInt(item1.attr) - parseInt(item2.attr);

であれば英数字その他の利用localCompare().

list.sort(function(item1, item2){
    return +(item1.attr > item2.attr) || +(item1.attr === item2.attr) - 1;
}) 

どのように作品サンプル:

+('aaa'>'bbb')||+('aaa'==='bbb')-1
+(false)||+(false)-1
0||0-1
-1

+('bbb'>'aaa')||+('bbb'==='aaa')-1
+(true)||+(false)-1
1||0-1
1

+('aaa'>'aaa')||+('aaa'==='aaa')-1
+(false)||+(true)-1
0||1-1
0
<!doctype html>
<html>
<body>
<p id = "myString">zyxtspqnmdba</p>
<p id = "orderedString"></p>
<script>
var myString = document.getElementById("myString").innerHTML;
orderString(myString);
function orderString(str) {
    var i = 0;
    var myArray = str.split("");
    while (i < str.length){
        var j = i + 1;
        while (j < str.length) {
            if (myArray[j] < myArray[i]){
                var temp = myArray[i];
                myArray[i] = myArray[j];
                myArray[j] = temp;
            }
            j++;
        }
        i++;
    }
    var newString = myArray.join("");
    document.getElementById("orderedString").innerHTML = newString;
}
</script>
</body>
</html>
var str = ['v','a','da','c','k','l']
var b = str.join('').split('').sort().reverse().join('')
console.log(b)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top