Tutorial

Getting Started with Macros

Macro Editor

User Interface

Menus and Toolbar

Editing Macro Statements

Expression Editor

Macro Debugging

How To

Using variables

Finding and modifying objects

Creating new page content

Asking for user input

Storing persistent data

Using binary data

Sorting objects

Macro menus

Bulleted and numbered lists

Sample Macros

Concepts

Expressions

Objects

Properties

Variables

Data Types

Arrays

Functions

Literals

Operators

Comments

Last updated on: March 19, 2023
Also available as a single HTML file

Onetastic Macro Documentation > How To > Sorting objects

Sorting objects

Macros can sort a set of objects by any property or value. This is done throught the SortObjects function. A simple sort macro looks like this:

$Paragraphs = QueryObjects("Paragraph", GetCurrentPage()) SortObjects($Paragraphs, "text", true)

This macro will sort every paragraph in the current page alphabetically (property "text" was provided in the second parameter) in ascending order (true was provided in the last parameter).

Custom Sort Orders

You can also define custom sort orders by making use of arrays. Below macro will sort each paragraph by the second letter that appears in the paragraph:

$Paragraphs = QueryObjects("Paragraph", GetCurrentPage()) $SecondLetters = Array() ExpandForEach ($Paragraph in $Paragraphs) Array_PushBack($SecondLetters, String_SubString($Paragraph.text, 1, 1)) SortObjects($Paragraphs, $SecondLetters, true)

For the custom sorting, the second parameter to SortObjects must be an array that has indices 0 to number of elements being sorted. Each corresponding value in the array will then be compared to determine the order.

Sorting Objects that aren't Siblings

When sorting objects that are not under the same parent object, they are sorted only within their siblings. For instance if you try to sort all pages in current notebook, they will be sorted within each section and they won't move between sections. Similarly if you sort paragraphs in a page, paragraphs in same outline will be sorted together but not across outlines. This is the desired behavior for most cases and will prevent simple mistakes to mix everything up on your pages or notebooks.

Reference

Statements

For

ForEach

If

Else If

Else

Switch

Case

Default

While

Expression

Comment

Break

Continue

Return

Hierarchy Objects

NotebookRoot

Notebook

SectionGroup

Section

Page

Page Objects

Title

Outline

Table

Column

Row

Cell

Paragraph

Text

Image

EmbeddedFile

Tag

Other Objects

DialogBox

MacroMenu

Window

Functions

Array Functions

Color Functions

Data Store Functions

Date/Time Functions

Dialog Box Functions

Macro Execution Functions

Macro Menu Functions

Object Functions

String Functions

Window Functions