문제
하나 이상의 열을 정렬 키로 사용하여 테이블을 정렬해야 합니다.
해결책
오름차순 및 내림차순과 내부 또는 새 테이블 정렬을 지원하는 테이블의 정렬 방법을 사용합니다.
// 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)) );
.
예를 들어 주셔서 감사합니다!