問題
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)) );
。
例をありがとう!