ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage functions / Standard functions / Queue, stack, list and array functions / Array functions
  • Miscellaneous
  • Example of custom sort procedure
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Sorts a WLanguage array. The following sorts can be performed:
  • Sort a one-dimensional array of simple elements.
  • Sort a two-dimensional array of simple elements according to one or more columns.
  • Sort a one-dimensional array of classes or structures according to 1 or more members.
  • WINDEVWEBDEV - Server code Sort an array of records according to 1 or more items.
  • Sort a one-dimensional array by specifying a comparison procedure.
Note: This function is equivalent to ArraySort.
Example
// Tri d'un tableau à une dimension
MonTableau is array of 2 int
MonTableau[1] = 4
MonTableau[2] = 1
Sort(MonTableau, asAscending)
Trace(MonTableau[1], MonTableau[2])
// Affiche "1" puis "4"
// Tri d'un tableau à 2 dimensions selon la 2ème colonne
Sort(MonTableau, asAscending, 2)
// Tri d'un tableau à 2 dimensions selon la 2ème colonne
// (croissant) et la 3ème colonne (décroissant)
Sort(MonTableau, asColumn, "2;-3")
// Tri d'un tableau en utilisant 1 procédure de comparaison
Sort(MonTableau, asFunction, Compare)
// Procédure de comparaison
PROCEDURE Compare(Elément1, Elément2)
IF Elément1.Membre < Elément2.Membre THEN RETURN -1
IF Elément1.Membre > Elément2.Membre THEN RETURN 1
RETURN 0
// Tri d'un tableau de type avancé en utilisant 1 procédure de comparaison
g is gglCalendar
Sort(g.Event, asFunction, TrieEvenementContenu)
// Procédure de comparaison
PROCEDURE TrieEvenementContenu(e1 is gglEvent, 
						e2 is gglEvent)
RETURN StringCompare(e1.Content, e2.Content)
Syntax

Sorting a one-dimensional array of simple elements Hide the details

Sort(<Array name> [, <Type of sort>])
<Array name>: Array
  • Name of the Array variable to use. This array must be a one-dimensional array.
    This array can also correspond to an array of simple elements of an advanced variable ("Group" array in the gglContact variables for example).
  • Name of the Associative Array variable to use.
    AndroidJavaPHP The associative arrays cannot be sorted in this version.
<Type of sort>: Optional integer constant
Type of sort to perform:
asAscending
(Default value)
Ascending sort.
Case of an associative array: Sorting the elements of the associative array:
  • if the constant ttClé is specified: keys are sorted in ascending order. In case of equality between two keys, the values are sorted according to the ascending value if the values can be compared. If the values cannot be compared, the initial order is kept.
  • if the constant ttKey is not specified Sorting is performed in ascending order of values. In case of equality between two values, the values are sorted according to the ascending key.
asDescendingDescending sort.
Case of an associative array: Sorting the elements of the associative array:
  • if the constant ttClé is specified: keys are sorted in descending order. In case of equality between two keys, the values are sorted according to the descending value if the values can be compared. If the values cannot be compared, the initial order is kept.
  • if the constant ttKey is not specified Sorting is performed in descending order of values. In case of equality between two values, the values are sorted according to the descending key.
The sort type can be combined with one or more sort options:
asKeyAssociative array sorted according to the key order. If this constant is not specified, the associative array is sorted according to the order of values.
This function is useful on the associative arrays only.
tccIgnoreAccentArray sorted while ignoring the accented characters.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreCaseArray sorted while ignoring the case.
This option is useful for arrays of strings.
tccIgnoreInsideSpaceArray sorted while ignoring the space characters found inside the strings.
This option is useful for arrays of strings.
tccIgnorePunctuationAndSpaceArray sorted while ignoring the space characters and the punctuation characters.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreSpaceArray sorted while ignoring the space characters found at the beginning and at the end of strings.
This option is useful for arrays of strings.
tccLexicographicOrderArray sorted according to the linguistic order.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccRespectNumericArray sorted according to the order of numeric values.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
AndroidJavaPHP These constants are not available.

Sorting a two-dimensional array of simple elements according to one column Hide the details

Sort(<Array name> [, <Type of sort>] , <Column>)
<Array name>: Array
Name of the Array variable to use. This array must be a two-dimensional array.
This array can also correspond to a two-dimensional array of an advanced type (sort an xlsDocument variable according to the values of a column for example).
<Type of sort>: Optional integer constant
Type of sort to perform:
asAscending
(Default value)
Ascending sort
asDescendingDescending sort
The sort type can be combined with one or more sort options:
tccIgnoreAccentArray sorted while ignoring the accented characters.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreCaseArray sorted while ignoring the case.
This option is useful for arrays of strings.
tccIgnoreInsideSpaceArray sorted while ignoring the space characters found inside the strings.
This option is useful for arrays of strings.
tccIgnorePunctuationAndSpaceArray sorted while ignoring the space characters and the punctuation characters.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreSpaceArray sorted while ignoring the space characters found at the beginning and at the end of strings.
This option is useful for arrays of strings.
tccLexicographicOrderArray sorted according to the linguistic order.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccRespectNumericArray sorted according to the order of numeric values.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
AndroidJavaPHP These constants are not available.
WEBDEV - Browser code This parameter must be specified.
<Column>: Integer
Index of column where the sort will be performed.

Sorting a two-dimensional array of simple elements according to several columns Hide the details

Sort(<Array name> , <asColumn> , <Columns>)
<Array name>: Array
Name of the Array variable to use. This array must be a two-dimensional array.
This array can also correspond to a two-dimensional array of an advanced type (sort performed on a variable of type xlsDocument according to the values of one or more columns for example).
<asColumn>: Constant
ttColumn Constant used to sort an array of simple two-dimensional elements into several columns.
The asColumn constant can be combined with one or more sort options:
tccIgnoreAccentArray sorted while ignoring the accented characters.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreCaseArray sorted while ignoring the case.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
tccIgnoreInsideSpaceArray sorted while ignoring the space characters found inside the strings.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
tccIgnorePunctuationAndSpaceArray sorted while ignoring the space characters and the punctuation characters.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccIgnoreSpaceArray sorted while ignoring the space characters found at the beginning and at the end of strings.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
tccLexicographicOrderArray sorted according to the linguistic order.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
tccRespectNumericArray sorted according to the order of numeric values.
This option is applied to all the columns on which the sort is performed.
This option is useful for arrays of strings.
WEBDEV - Browser code This constant is not available.
AndroidJavaPHP These constants are not available.
<Columns>: Character string
List of columns in which the sort must be performed. The different columns must be separated by a semicolon. To specify the sort direction, the index of the column must be preceded by:
  • "+" (or nothing) for an ascending sort.
  • "-" for a descending sort.
    For example: "2;-3" to perform an ascending sort on the second column and a descending sort on the third column.

Sorting a one-dimensional array of classes, structures, advanced types or records Hide the details

Sort(<Array name> , <asMember> , <Members>)
<Array name>: Array
  • Name of the Array variable to use. This array must be a one-dimensional array. This array can be:
    • an array of classes,
    • an array of structures,
    • an array of advanced types,
    • WINDEVWEBDEV - Server code an array of records.
  • Name of the Associative Array variable to use. This array can be:
    • an associative array of classes,
    • an associative array of structures,
    • an associative array of advanced types,
      AndroidJavaPHP The associative arrays cannot be sorted in this version.
    • WINDEVWEBDEV - Server code an associative array of records.
<asMember>: Constant
ttMember Constant for sorting a one-dimensional array of classes, structures or advanced types.
Case of an associative array The elements of the associative array are sorted according to the order of the values of the specified members. In case of equality between two values, the values are sorted according to the ascending or descending key (according to the sort direction of the first member).
The asMember constant can be combined with one or more sort options:
tccIgnoreAccentArray sorted while ignoring the accented characters.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
WEBDEV - Browser code This constant is not available.
tccIgnoreCaseArray sorted while ignoring the case.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
tccIgnoreInsideSpaceArray sorted while ignoring the space characters found inside the strings.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
tccIgnorePunctuationAndSpaceArray sorted while ignoring the space characters and the punctuation characters.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
WEBDEV - Browser code This constant is not available.
tccIgnoreSpaceArray sorted while ignoring the space characters found at the beginning and at the end of strings.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
tccLexicographicOrderArray sorted according to the linguistic order.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
WEBDEV - Browser code This constant is not available.
tccRespectNumericArray sorted according to the order of numeric values.
This options is applied to all the members on which the sort is performed.
This option is useful for the "character string" members.
WEBDEV - Browser code This constant is not available.
AndroidJavaPHP These constants are not available.
<Members>: Character string
Name of members used as sort criteria. If the sort is performed on several members, their names must be separated by a semicolon. To specify the sort direction, the name of the member must be preceded by:
  • "+" (or nothing) for an ascending sort.
  • "-" for a descending sort.
  • "." or ":" to perform a sort on chained properties.
    For example:
    - "Member1;-Member3" to perform an ascending sort on the Member1 member and a descending sort on the Member3 member.
    - "Source.URL" to perform a sort on the URL property of the Source property.
WINDEVWEBDEV - Server code Sort an array of records Name of sort items used as sort criteria.

Sorting a one-dimensional array with a sort procedure Hide the details

Sort(<Array name> , <asFunction> , <Custom WLanguage procedure>)
<Array name>: Array
  • Name of the Array variable to use. This array must be a one-dimensional array. This array can be an array of advanced types.
  • Name of the Associative Array variable to use.
    AndroidJavaPHP The associative arrays cannot be sorted in this version.
<asFunction>: Constant
ttFunction: Constant used to sort a one-dimensional array using a sorting procedure.
<Custom WLanguage procedure>: Character string
  • Case of a one-dimensional array: WLanguage procedure of the form:
    PROCEDURE MyProcedure(Element1, Element2)

    This procedure is automatically called by the sort mechanism to compare the elements between themselves, two by two.
    If the element 1 must be found before the element 2 in the sort order, the procedure must return -1.
    If the element 1 must be found after the element 2 in the sort order, the procedure must return 1.
    If element 1 and element 2 are identical, the procedure must return 0.
  • Case of an associative array: WLanguage procedure of the form:
    PROCEDURE MyProcedure(Value1, Key1, Value2, Key2)

    This procedure is automatically called by the sort mechanism to compare the elements between themselves, two by two. The comparison can be performed in the sort procedure according to the values or according to the keys.
    If the element 1 must be found before the element 2 in the sort order, the procedure must return -1.
    If the element 1 must be found after the element 2 in the sort order, the procedure must return 1.
    If element 1 and element 2 are identical, the procedure must return 0.
Remarks

Miscellaneous

  • This function cannot be used with the fixed arrays.
  • Dynamic array of variants: Only custom sorting (with a sorting procedure) can be used.
  • This function cannot be used with arrays of Unicode strings.
  • To add an element into a sorted array, use ArrayAddSorted.
  • This function can be used on the arrays of advanced variables.
  • This function is available for the associative arrays.
    AndroidJavaPHP This feature is not available.

Example of custom sort procedure

Example of procedure used to compare 2 integers:
PROCEDURE MaProcédure(nEntier1, nEntier2)
IF nEntier1 > nEntier2 THEN RETURN 1
IF nEntier1 < nEntier2 THEN RETURN -1
RETURN 0
Example of a procedure used to compare two elements in an array of structures:
PROCEDURE MaProcédure(stVar1, stVar2)
IF stVar1:nEntier > stVar2:nEntier THEN RETURN 1 
IF stVar1:nEntier < stVar2:nEntier THEN RETURN -1 
RETURN 0
Component: wd300vm.dll
Minimum version required
  • Version 14
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 03/28/2025

Send a report | Local help