问题
您需要使用一列或多列作为排序键对表进行排序。
解决方案
使用表的排序方法,支持升序和降序以及就地或新表排序。
// 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)) );
。
谢谢你的例子!