文字列の比較においてreturn文で-1,0,1を返す処理は、
下記の編集後と同様の内容を行っていると解釈していいのでしょうか?
よろしくお願い致します。
改訂新版jQuery本格入門 P191より引用
HTML5
1<body> 2 <div> 3 <h4 id="head-1">jQuery</h4> 4 <h4 id="head-2">jQuery UI</h4> 5 <h4 id="head-3">JavaScript</h4> 6 <h4 id="head-4">CSS</h4> 7 <h4 id="head-5">HTML</h4> 8 </div> 9</body> 10``` 11 12```jQuery 13$(function() { 14 15 // <h4 id="head-3">以降を抽出 16 const webtech = $('h4').slice(2); 17 webtech.length; 18 19 // 要素を追加 20 const size = webtech.push($('<h4 id="head-6">Flash</h4>')[0]); 21 22 // <h4 id="head-4">、<h4 id="head-5">を新たなDOMオブジェクトと入れ替え 23 webtech.splice(1, 2, 24 $('<h4 id="head-4">CSS3</h4>')[0], 25 $('<h4 id="head-5">HTML5</h4>')[0] 26 ); 27 28 // テキストの内容に基づいて並び替え 29 webtech.sort(function(a, b) { 30 const at = $(a).text(); 31 const bt = $(b).text(); 32 if (at < bt) { // @@@@@@@@@@ 疑問の箇所 @@@@@@@@@@@@ 33 return -1; 34 } else if (at > bt){ 35 return 1; 36 } else { 37 return 0; 38 } 39 }); 40 41 $('div') 42 .empty() 43 .append(webtech); 44}); 45``` 46```jQuery 47// 編集したコード ← ここは間違いであることが指摘されました 48webtech.sort(function(a, b) { 49 const at = $(a).text(); 50 const bt = $(b).text(); 51 return at - bt; 52}); 53``` 54
回答2件
あなたの回答
tips
プレビュー