message.sortJSONArrayByString¶
Signature: message.sortJSONArrayByString(nodePathToArray, sortNodePath, descending*)
Returns: void
Sorts objects inside a JSON array alphabetically based on the text value of the sortNodePath inside the objects in the array.
Note
This method only applies to JSON formatted messages.
Parameters¶
| Type | Name | Description | Default |
|---|---|---|---|
| String | nodePathToArray | the JSON array to sort | |
| String | sortNodePath | the text data element within the JSON object that provides the basis for the sorting of the objects in the array. Must be a full path location of the sub-array object. | |
| Boolean | descending* | (optional) when true sorts in descending order | false |
Example¶
// Example: Sort objects inside a JSON array by a string field.
// Sample JSON message structure
message = qie.createJSONMessage(JSON.stringify(
{
message: {
people: [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
{ name: "Charlie", age: 35 }
]
}
}));
// Sort array by string value (ascending)
message.sortJSONArrayByString(
"/message/people", // array path
"/name" // string sort key
);
var firstAfterAsc = message.getNode("/message/people[1]/name"); // "Alice"
var lastAfterAsc = message.getNode("/message/people[3]/name"); // "Charlie"
// Sort array by string value (descending)
message.sortJSONArrayByString(
"/message/people", // array path
"/name", // string sort key
true // descending
);
var firstAfterDesc = message.getNode("/message/people[1]/name"); // "Charlie"
var lastAfterDesc = message.getNode("/message/people[3]/name"); // "Alice"