Promotic

Sort - method of the PmArray object

Description:
Allows alphabetical or numeric sorting.
The column order can be defined for sorting of 2-dimensional arrays.
Syntax:
Empty Sort(String sType, [Function onSort])
Parameters:
sType(String) Sorting type.
If the sorting is executed by the function defined in the onSort parameter, then an empty string "" must be here.
Otherwise enter the string depending on the array type:
- 1-dimensional array:
"tn" - numeric (=n), ascending
"tn+r" - numeric (=n), descending (=r)
"ts" - alphabetic, ascending, by the system, not depending on the active language (=s), case sensitive text
"ts+r" - alphabetic, descending (=r), by the system, not depending on the active language (=s), case sensitive text
"tsi" - alphabetic, ascending, by the system, not depending on the active language (=s), case insensitive text (=i)
"tsi+r" - alphabetic, descending (=r), by the system, not depending on the active language (=s), case insensitive text (=i)
"tl" - alphabetic, ascending, localized, based on the active language (=l), case sensitive text
"tl+r" - alphabetic, descending (=r), localized, based on the active language (=l), case sensitive text
"tli" - alphabetic, ascending, localized, based on the active language (=l), case insensitive text (=i)
"tli+r" - alphabetic, descending (=r), localized, based on the active language (=l), case insensitive text (=i)
- 2-dimensional array:
When sorting by a single column, the column index is entered first (c0+, c1+, ...), followed by the sorting method (same as for a 1-dimensional array). So sorting by a particular column might look like this, for example: c0+ts.
 
When sorting by multiple columns simultaneously, the sorting rules for each column shall be listed in order of importance, separated by a comma (,). For example: "c1+ts,c0+tn+r". The resulting array will then be sorted by the second column alphabeticaly ascending and identical items will then be sorted by numeric values of the first column descending.
onSort[optional](Function) The onSort parameter contains function, which allows custom sorting of the 1-dimensional array where the default sorting methods are not sufficient.
Function onSort user-specifically compares two array items against each other.
This function is called repeatedly during sorting and must be in the form:
function onSort(v1, v2) { return v1 < v2 ? -1 : v1 > v2 ? 1 : 0; }
where the v1 parameter and the v2 parameter contain two array values that must be compared against each other.
The function returns:
-1 - if v1 < v2
1 - if v1 > v2
0 - if v1 = v2.
Note! If the the onSort parameter is used then the sType parameter must be the empty string "".
Note:
This method is also functional in Web panels.

If some sorted values are identical then their order in the array cannot be guaranteed.
Example1:
Sorting the 1-dimensional array numericaly, ascending:
JavaScriptSelect and copy to clipboard

var arr = Pm.CreatePmArray().Array1(0.5, 0.1, 123, -3, -1, 0, 0.8, 0.2);
arr.Sort("tn");
// the array will be sorted as follows: -3, -1, 0, 0.1, 0.2, 0.5, 0.8, 123
Example2:
Sorting the 1-dimensional array alphabeticaly, ascending:
JavaScriptSelect and copy to clipboard

var arr = Pm.CreatePmArray().Array1("Madrid", "Berlin", "Atheny", "Praha", "Warszava", "Paris", "Vien", "Roma");
arr.Sort("ts");
// the array will be sorted as follows: "Atheny, Berlin, Madrid, Paris, Praha, Roma, Vien, Warszava"
Example3:
descending (=r), localized, based on the active language (=l), case insensitive text (=i)
JavaScriptSelect and copy to clipboard

var arr = Pm.CreatePmArray().Array1("acg1", "ach1", "ACJ1");
arr.Sort("tli+r");
Example4:
Custom sorting the 1-dimensional array:
JavaScriptSelect and copy to clipboard

function onSort(v1, v2)
{
return v1 < v2 ? -1 : v1 > v2 ? 1 : 0;
}

var arr = Pm.CreatePmArray().Array1(0.5, 0.1, 123, -3, -1, 0, 0.8, 0.2);
arr.Sort("", onSort);
// the array will be sorted as follows: -3, -1, 0, 0.1, 0.2, 0.5, 0.8, 123

History:
Pm9.00.18: New types of localized sorting based on the application active language (tl, tli, tl+r and tli+r) in the sType parameter.
Pm9.00.17: New onSort parameter to sort a 1-dimensional array using a function.
Pm9.00.09: Created
PROMOTIC 9.0.21 SCADA system documentation - MICROSYS, spol. s r.o.

Send page remarkContact responsible person
© MICROSYS, spol. s r. o.