Problema
Devi ordinare una tabella utilizzando una o più colonne come chiave di ordinamento.
Soluzione
Utilizzare il metodo di ordinamento della tabella, che supporta l'ordine ascendente e discendente e l'ordinamento sul posto o nuovo della tabella.
// 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 );
Discussione
L'esempio finale mostra l'aggiunta di una nuova colonna per acquisire l'ordinamento originale e come utilizzare quella nuova colonna per ripristinare l'ordine originale. La documentazione mostra un'altra utile opzione per denominare la tabella.
new table = dt << sort( by( height, weight ), Output Table Name("Fred") );
Guarda anche
Documentazione
Buon punto. Piuttosto che valutare una formula per ogni riga, quindi rimuovere la formula, basta costruire la sequenza come una matrice (l'operatore ::) e inizializzare direttamente la colonna. Grazie!
penso che la dichiarazione
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 (!)
essere più efficiente come
dt << New Column( "original row", set values( 1::(N Rows(dt)) );
.
Grazie per l'esempio!