TSV String, Sort by Text
TSV String, Sort by Text
Sorts TSV lines in the order of the character code of the specified column. If the column value for each line is Jan Feb March, the text is sorted in the order of “line that contains Feb”, “line that contains Jan” and “line that contains March”.
Configs
  • A: Select STRING DATA for TSV String *
  • B-key: Set Column ID(s) for Sort Target (eg “0” “3,1” ) *#{EL}
  • B-order: Set DESC or ASC ( default “DESC” )#{EL}
  • C: Select STRING DATA for Sorted TSV (update) *
Script (click to open)
// GraalJS Script (engine type: 2)
// (c) 2021, Questetra, Inc. (the MIT License)
// Notes:
// If specifying multiple sort keys, use {Column ID} in CSV notation. (eg "3,1,0")
// Notes(ja):
// ソートキーを複数指定したい場合、{Column ID} をCSV表記で行います ( 例 "3,1,0" )



//////// START "main()" ////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA      = configs.get( "conf_DataIdA" ) + "";      // config required
const targetColIds = configs.get( "conf_TargetColIds" ) + ""; // config required
let   sortOrder    = configs.get( "conf_SortOrder" ) + "";    // config not required
const dataIdC      = configs.get( "conf_DataIdC" ) + "";      // config required
engine.log( " AutomatedTask Config: Sort Key Column(s): " + targetColIds );
engine.log( " AutomatedTask Config: Sort Order: " + sortOrder );

if( targetColIds === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Sort Column} is empty \n" );
}
const arrTargetColIds = targetColIds.split(",");

if( sortOrder !== "ASC" ){
  sortOrder = "DESC";
}


//// == Data Retrieving / ワークフローデータの参照 ==
const inputString = engine.findDataByNumber( dataIdA ) + "";
let   arrInputString = inputString.split("\n");
engine.log( " AutomatedTask MultilineString:" + 
            " TSV {A}, number of lines: " + arrInputString.length );


//// == Calculating / 演算 ==
arrInputString.sort( function( tsvlineA, tsvlineB ){
  let arrTsvlineA = tsvlineA.split("\t");
  let arrTsvlineB = tsvlineB.split("\t");

  if( sortOrder === "DESC" ){
    for( let i = 0; i < arrTargetColIds.length; i++ ){
      if( arrTsvlineA.length <= arrTargetColIds[i] ||
          arrTsvlineB.length <= arrTargetColIds[i] ){
        throw new Error( "\n AutomatedTask UnexpectedNumberError:" +
                         " Number {Sort Key ID} is larger than TSV length \n" );
      }
      if( arrTsvlineA[ parseInt(arrTargetColIds[i]) ] <
          arrTsvlineB[ parseInt(arrTargetColIds[i]) ] ){ return 1; }
      if( arrTsvlineA[ parseInt(arrTargetColIds[i]) ] >
          arrTsvlineB[ parseInt(arrTargetColIds[i]) ] ){ return -1; }
    }
  }else{ // ASC: alphabetical from A to Z
    for( let i = 0; i < arrTargetColIds.length; i++ ){
      if( arrTsvlineA.length <= arrTargetColIds[i] ||
          arrTsvlineB.length <= arrTargetColIds[i] ){
        throw new Error( "\n AutomatedTask UnexpectedNumberError:" +
                         " Number {Sort Key ID} is larger than TSV length \n" );
      }
      if( arrTsvlineA[ parseInt(arrTargetColIds[i]) ] >
          arrTsvlineB[ parseInt(arrTargetColIds[i]) ] ){ return 1; }
      if( arrTsvlineA[ parseInt(arrTargetColIds[i]) ] <
          arrTsvlineB[ parseInt(arrTargetColIds[i]) ] ){ return -1; }
    }
  }
  return 0;
});


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, arrInputString.join("\n") );

} //////// END "main()" ////////////////////////////////////////////////////////////////

Download

The Add-on import feature is available with Professional edition.

Notes

  1. If specifying multiple sort keys, use {Column ID} in CSV notation. (eg “3,1,0”)

Capture

Sorts TSV lines in the order of the character code of the specified column. If the column value for each line is Jan Feb March, the text is sorted in the order of "line that contains Feb", "line that contains Jan" and "line that contains March".

See also

2 thoughts on “TSV String, Sort by Text”

  1. Pingback: TSV String, Sort by Text – Questetra Support

  2. Pingback: TSV String, Sort by Numeric – Questetra Support

Comments are closed.

%d bloggers like this: