問題
您需要使用一個或多個列作為排序關鍵字對錶進行排序。
解決方案
使用表格的排序方法,支持升序和降序以及就地或新表格排序。
// 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)) );
.
感謝您的示例!