APL/J言語:並べ替え
並べ替え(ソート)関数/:(スラッシュコロン)は、x /: yとしたときyのグレードの順にxを並べ替える。yのグレードは/: yで確認できる。
x=: 2 7 1 8 [ y=: 1 7 3 2 (/:y);((/:y){x);(x/:y);(x/:x) +-------+-------+-------+-------+ |0 3 2 1|2 8 1 7|2 8 1 7|1 2 7 8| +-------+-------+-------+-------+ NB.x自身の並べ替え(昇順)はx/:xとなる
文字列の並べ替え順序はアルファベットa.の順である。
以下の例は次の文章を対象にしたものである。
For example, if the name “text” is used for the present sentence (up to and including the colon), then:
text=:'For example, if the name "text" is used for the present sentence (up to and including the colon), then:' tdw=: >dwds=: ~. wds=: ;: text ($tdw),($dwds),($wds),($text) 21 9 21 25 103 ]alph=: a. {~ ,(i. 26) +/ (a.i.'aA') aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ tdw; (tdw /: tdw);(tdw/: alph i. tdw) +---------+---------+---------+ |For |" |and | |example |( |colon | |, |) |example | |if |, |for | |the |For |For | |name |and |if | |" |colon |including| |text |example |is | |is |for |name | |used |if |present | |for |including|sentence | |present |is |text | |sentence |name |then: | |( |present |the | |up |sentence |to | |to |text |up | |and |the |used | |including|then: |, | |colon |to |" | |) |up |( | |then: |used |) | +---------+---------+---------+
中央の列は単語をアスキーコード順に並べたものだが、大文字小文字の処理がなされていないので、"for"と"For"の位置が大きく離れてしまっている。三番目の列はアルファベット順になるようにテーブル処理をしたものである。