Promotic
WikipediaLinkedInYoutubeTwitterFacebook

Sort - method of the PmArray object

Description:
Allows alphabetical or numeric sorting of 1-dimensional array or 2-dimensional array. The column order can be defined for sorting of 2-dimensional arrays.
Syntax:
Empty Sort(String sType, [Function onSort])
Parameters:
sType(String)
- 1-dimensional array:
"tn" - numeric ascending
"tn+r" - numeric descending
"ts" - alphabetic ascending (case sensitive text)
"ts+r" - alphabetic descending (case sensitive text)
"tsi" - alphabetic ascending (i = case insensitive text)
"tsi+r" - alphabetic descending (i = case insensitive text)
- 2-dimensional array:
"c0+tn" - sorting by 1st (zero) column, numeric, ascending
"c0+ts" - sorting by 1st (zero) column, alphabetic, ascending (case sensitive text)
"c0+tsi" - sorting by 1st (zero) column, alphabetic, ascending (i = case insensitive text)
 
The order and sorting method can be easily combined, 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 a 1-dimensional array where the default sorting methods are not sufficient. Function onSort user-specifically compares two array entries 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:
Custom sorting of 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.17: New parameter onSort to sort a 1-dimensional array using a function.
Pm9.00.09: Created
PROMOTIC 9.0.17 SCADA system documentation - MICROSYS, spol. s r.o.

Send page remarkContact responsible person
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice