Usina BRAdvPL Guide
Sign in
← All topics
FUNCTIONUnder review

ASort

ASort( <aVetor>, [nInicio], [nCont], [bOrdem] ) --> aRetorno

Sorts an array or range with an optional comparator.

ASortAdvPL
01 · OVERVIEW

Overview

ASort changes the array itself and returns its reference. Default order is ascending for comparable element types. For matrices its block receives two elements and indicates whether the first precedes the second. Documentation does not guarantee stable ordering of ties; add a tie-breaker when required. The comparator may run many times and must not write data or modify compared elements. (TOTVS, s.d., ref. 1)

02 · SYNTAX

Syntax

ASort( <aVetor>, [nInicio], [nCont], [bOrdem] ) --> aRetorno

Parameters

aVetor
ArrayRequired

Array to sort.

nInicio
NumericOptional

Start; default 1.

nCont
NumericOptional

Count; default through the end.

bOrdem
Code blockOptional

Comparator receiving two elements.

Return value

Reference to the sorted array.

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function EdAsort()
    Local aItems := {{"B", 20}, {"A", 10}}
    // Compare the text key without modifying records.
    ASort(aItems, , , {|aLeft, aRight| aLeft[1] < aRight[1]})
    ConOut(aItems[1][1])
Return Nil
Expected result

Expected first key: A. The original collection is reordered. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Do not use <= as a strict comparator; ensure consistency and valid types. Clone first when original ordering matters.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. ASort. TDN. Accessed on 2026-10-26.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha