タグ

sortに関するedvakfのブックマーク (4)

  • comparefn follow-up - ellaneous

    色々とおかしかったので前回の記事を訂正した。 テスト用の配列を「Math.random()」で作成したのが明らかな間違い(0 を返すべきペアがほぼ生じないのだから「-.5 or .5」でソートできるのは当然)で「Math.random() > .1」に変更して要素数を増やしたところ SpiderMonkey/Rhino/JScript でのみ有効という残念な結果になった。 蛇足 なぜダメなのかを探る。幸い Google Chrome は sort の中身が丸見えである。 javascript:'<pre>'+[].sort function sort(comparefn) { var custom_compare = (typeof(comparefn) === 'function'); function Compare(x,y) { if (custom_compare) { retur

    comparefn follow-up - ellaneous
  • sort( )について少し詳しく - ひきメモ

    デフォルトでは、文字列なら文字コード順、数値なら値の小さい順に並び替えます。リストやタプルの場合は、0番目の要素で並び替えを行います。 >>> list = [94, 25, 9, 67] >>> list.sort() >>> list [9, 25, 67, 94] >>> >>> list = ["banana", "grape", "orange", "apple"] >>> list.sort() >>> list ['apple', 'banana', 'grape', 'orange'] o>>> >>> list = [(3, 99), (4, 95), (1, 123), (2, 14)] >>> list.sort() >>> list [(1, 123), (2, 14), (3, 99), (4, 95)] cmpキーワード >>> list = ["98", "1

    sort( )について少し詳しく - ひきメモ
  • comparefn - ellaneous

    Array.prototype.sort が受け取る比較関数は comparefn が undefined でないならば、それは 2 個の引数 x と y を受け付け、 x < y ならば負の値、 x = y ならば 0、 x > y ならば正の値を返す関数であるべきである。 http://www2u.biglobe.ne.jp/~oz-07ams/prog/ecma262r3/15-4_Array_Objects.html#section-15.4.4.11 という仕様で,これを真面目に書くと // sorting by 'str' property arr.sort(function(x, y){ return x.str > y.str ? -1 : x.str < y.str ? 1 : 0 } のようになりえらく辛気臭い。 来ソートするために必要な情報はその並びが正しいか否かの

    comparefn - ellaneous
  • Sorting Algorithms Animations

    KEY Black values are sorted. Gray values are unsorted. A red triangle marks the algorithm position. Dark gray values denote the current interval (shell, merge, quick). A pair of red triangles marks the left and right pointers (quick). DISCUSSIONThese pages show 8 different sorting algorithms on 4 different initial conditions. These visualizations are intended to: Show how each algorithm operates.

    Sorting Algorithms Animations
    edvakf
    edvakf 2009/01/22
  • 1