Sorts the specified vector according to the order induced by the keys
generated for each element by the specified key subquery.
The keys are compared as described in compare()
function.
Effectively, the element comparator that determines the order of the vector may be expressed as the following:
compare (keyQuery(elem1), keyQuery(elem2))
v
elemVar
The variable reference must be produced using '@'
operator
(see examples below).
keyQuery
The subquery should be prepared using FlexQuery()
function
(see examples below).
unique
If true
all elements with the repeating
(according to compare()
function) keys will be removed;
if false
all original elements will be present in the result vector.
When this parameter is omitted, it is assumed to be false
.
Note: If the passed vector object is null
,
the function does nothing and returns null
.
compare(), FlexQuery(), reverseVector(), filterVector()
The following expression sorts a vector containing string representations of numbers according to the number values and prints the result vector to the system console:
// the original vector
v = Vector ("5", "103", "12", "1");
// sorting the vector
v.sortVector (
@elem,
FlexQuery (toNumber(elem))
);
echo (v) // print the result vector
The original vector contains strings each of which is started with a certain letter followed by a certain number. We want to sort those strings so as the letter parts are ordered lexicographically and numbers parts as numbers.
The following expression does such a sorting and prints the result vector to the system console:
// the original vector
v = Vector ("B5", "C103", "C12", "A1", "B5");
// sorting the vector
v.sortVector (
@elem,
FlexQuery ({
s = toString(elem);
Array (
s.charAt(0),
s.substring(1).toNumber()
)
}),
true
);
// print the result vector, which will be:
// { "A1", "B5", "C12", "C103" }
echo (v)