ArraySort
Sorts array elements.
Method Signature
ArraySort(array=[modifiablearray], sortType=[any], sortOrder=[string], localeSensitive=[boolean], callback=[function:Comparator])
Arguments
array
modifiablearray
true
The array to sort
sortType
any
false
Options are text, numeric, or textnocase
textnocase
sortOrder
string
false
Options are asc or desc
asc
localeSensitive
boolean
false
Sort based on local rules
callback
function:Comparator
false
Function to sort by
Examples
Simple example for arraySort function
Uses the arraySort() function to get the sorted array and which sorted by type numeric
someArray = [
10,
20,
-99,
46,
50
];
arraySort( someArray, "numeric", "desc" );
writeOutput( JSONSerialize( someArray ) );
Result: [50,46,20,10,-99]
Simple example with member function
someArray = [
"BOXLANG",
"boxlang",
"adobe",
"Boxlang",
"RAILO"
];
someArray.sort( "text", "desc" );
writeOutput( JSONSerialize( someArray ) );
Result: ["boxlang","adobe","RAILO","Boxlang","BOXLANG"]
Simple example with callback function
Uses the callback function
someArray = [
{
NAME : "testemployee",
AGE : "32"
},
{
NAME : "employeetest",
AGE : "36"
}
];
arraySort( someArray, ( Any e1, Any e2 ) => {
return compare( e1.NAME, e2.NAME );
} );
writeOutput( JSONSerialize( someArray ) );
Result: [{"NAME":"employeetest","AGE":"36"},{"NAME":"testemployee","AGE":"32"}]
Script member syntax: sort array of structs by multiple keys
Takes an array of structs and sorts by multiple different keys, similar to the way a query allows.
arrayOfStructs = [
{
"userId" : 1,
"firstName" : "John",
"lastName" : "Doe",
"departmentName" : "Sales",
"active" : 1
},
{
"userId" : 2,
"firstName" : "Jane",
"lastName" : "Smith",
"departmentName" : "Marketing",
"active" : 1
},
{
"userId" : 3,
"firstName" : "Alice",
"lastName" : "Johnson",
"departmentName" : "Sales",
"active" : 0
},
{
"userId" : 4,
"firstName" : "Bob",
"lastName" : "Brown",
"departmentName" : "Sales",
"active" : 1
},
{
"userId" : 5,
"firstName" : "Charlie",
"lastName" : "Davis",
"departmentName" : "Marketing",
"active" : 0
}
];
arrayOfStructs.sort( ( Any user1, Any user2 ) => {
if( user1.ACTIVE != user2.ACTIVE ) {
return user2.ACTIVE - user1.ACTIVE;
}
if( user1.DEPARTMENTNAME == user2.DEPARTMENTNAME || user1.ACTIVE == 0 ) {
if( user1.LASTNAME == user2.LASTNAME ) {
return user1.FIRSTNAME.compare( user2.FIRSTNAME );
}
return user1.LASTNAME.compare( user2.LASTNAME );
}
return user1.DEPARTMENTNAME.compare( user2.DEPARTMENTNAME );
} );
writeDump( arrayOfStructs );
Result: Sorts by active employees, then by their last name and finally by their first name
Additional Examples
SomeArray = [
"BOXLANG",
"boxlang",
"adobe",
"Boxlang",
"BOXLANG"
];
arraySort( SomeArray, "text", "desc" );
dump( SomeArray );
// member function
SomeArray.sort( "text", "desc" );
dump( SomeArray );
SomeArray = [
{
NAME : "testemployee",
AGE : "32"
},
{
NAME : "employeetest",
AGE : "36"
}
];
arraySort( SomeArray, ( Any e1, Any e2 ) => {
return compare( e1.NAME, e2.NAME );
} );
dump( SomeArray );
// member function with closure
SomeArray.sort( ( Any e1, Any e2 ) => {
return compare( e1.NAME, e2.NAME );
} );
dump( SomeArray );
Related
Last updated
Was this helpful?