問題
1 つ以上の列をソート キーとして使用して、テーブルをソートする必要があります。
解決
昇順と降順、インプレースまたは新しいテーブルの並べ替えをサポートする、テーブルの並べ替え方法を使用します。
// run these lines, one at a time, to see the effects
// (place the cursor on the line and use the Enter key
// on the numeric keypad to easily submit the line.)
dt = Open( "$sample_data/big class.jmp" );
// example 1: makes a new table
new table = dt << sort( by( height, weight ) );
Wait( 2 );
Close( new table, nosave );
// example 2: updates table in place
dt << sort( by( height, weight ), order( descending ), replace table );
Wait( 2 );
Close( dt, nosave );
// example 3: keep a column of original row order
dt = Open( "$sample_data/big class.jmp" );
orig = dt << New Column( "original row", formula( Row() ) );
dt << runFormulas; // make sure the background formula evaluation is done, then
orig << deleteFormula; // delete the formula so it won't run after the sort (!)
dt << sort( by( height, weight ), order( descending ), replace table );
Wait( 2 );
dt << sort( by( orig ), order( ascending ), replace table );
Wait( 2 );
Close( dt, nosave );
討論
最後の例では、新しい列を追加して元の並べ替え順序を取得し、その新しい列を使用して元の順序を復元する方法を示します。 ドキュメントには、テーブルに名前を付けるための別の便利なオプションが示されています。
new table = dt << sort( by( height, weight ), Output Table Name("Fred") );
関連項目
ドキュメンテーション
いい視点ね。 行ごとに数式を評価してから数式を削除するのではなく、シーケンスをマトリックス (:: 演算子) として構築し、列を直接初期化します。 ありがとう!
私はその声明を考える
orig = dt << New Column( "original row", formula( Row() ) );
dt << runFormulas; // make sure the background formula evaluation is done, then
orig << deleteFormula; // delete the formula so it won't run after the sort (!)
としてより効率的になる
dt << New Column( "original row", set values( 1::(N Rows(dt)) );
.
例をありがとう!