Skip to main content
Version: Java (Groovy)

mergeSorted

mergeSorted combines two or more tables into one sorted, aggregate table. This essentially stacks the tables one on top of the other and sorts the result. Null tables are ignored. mergeSorted is more efficient than using merge followed by sort.

Syntax

mergeSorted(keyColumn, tables...)

Parameters

ParameterTypeDescription
keyColumnString

The column by which to sort the merged table.

tablesTable...

Source tables to be merged.

  • The tables to be merged must include the same columns of the same type.
  • Each table must be already sorted.
  • Null inputs are skipped.
tablesCollection<Table>

Source tables to be merged.

  • The tables to be merged must include the same columns of the same type.
  • Each table must be already sorted.
  • Null inputs are skipped.

Returns

A new table with the source tables stacked one on top of the other and sorted by the specified column.

Examples

In the following example, source1 is stacked on top of source2, and the result is sorted based on the Number column.

source1 = newTable(col("Letter", "A", "D", "E"), col("Number", 3, 4, 7))
source2 = newTable(col("Letter", "B", "C", "D"), col("Number", 1, 2, 5))

result = mergeSorted("Letter", source1, source2)

In the following example, three tables are merged and sorted based on the Number column.

source1 = newTable(col("Letter", "A", "C", "G"), col("Number", 1, 6, 9))
source2 = newTable(col("Letter", "B", "D", "G"), col("Number", 3, 5, 8))
source3 = newTable(col("Letter", "D", "E", "F"), col("Number", 2, 4, 7))

result = mergeSorted("Number", source1, source2, source3)