Problema
Debe ordenar una tabla usando una o más columnas como clave de ordenación.
Solución
Utilice el método de clasificación de la tabla, que admite el orden ascendente y descendente y la clasificación de tablas nuevas o in situ.
// 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 );
Discusión
El último ejemplo muestra cómo agregar una nueva columna para capturar el orden de clasificación original y cómo usar esa nueva columna para restaurar el orden original. La documentación muestra otra opción útil para nombrar la tabla.
new table = dt << sort( by( height, weight ), Output Table Name("Fred") );
Ver también
Documentación
Buen punto. En lugar de evaluar una fórmula para cada fila, luego elimine la fórmula, simplemente construya la secuencia como una matriz (el operador ::) e inicialice la columna directamente. ¡Gracias!
Creo que la declaración
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 (!)
ser más eficiente como
dt << New Column( "original row", set values( 1::(N Rows(dt)) );
.
¡Gracias por el ejemplo!