Macros/Calc/ba023

Summary
This page provides a LibreOffice Basic macro procedure that sorts a table based on the values in one column. It is an example drawn from Chapter 13 ("Calc as a Database") of the 7.0 Calc Guide.

Description
In a Calc macro, data within a range is sorted by calling the sort method on the range object. When a sort operation is called on a range, an array of properties known as a sort descriptor is passed to the sort method. Contained within a descriptor’s properties are sort fields, which are structures that inform Calc how to sort a range according to the data contained within one of its rows or columns.

As an example, the macro below sorts a class grade sheet in cells $Sheet1.A1:H11 according to average grade in descending order. The linked spreadsheet contains such a grade sheet.

Code
This [[Media:Sorting_a_table_using_one_column_with_a_macro.ods|Calc spreadsheet]] contains the above LibreOffice Basic code.