message.sortJSONArrayByDate¶
Signature: message.sortJSONArrayByDate(nodePathToArray, sortNodePath, descending*)
Returns: void
Sorts objects inside a JSON array by based on the date 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 date 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 date field.
// Sample JSON message structure
message = qie.createJSONMessage(JSON.stringify(
{
message: {
people: [
{ name: "Alice", age: 30, dob: "1993-04-17" },
{ name: "Bob", age: 25, dob: "1997-02-14" },
{ name: "Chris", age: 35, dob: "1975-12-25" }
]
}
}));
// Sort array by date value (ascending)
message.sortJSONArrayByDate(
"/message/people", // array path
"/dob" // date sort key
);
var firstAfterAsc = message.getNode("/message/people[1]/name"); // "Chris"
var lastAfterAsc = message.getNode("/message/people[3]/name"); // "Bob"
// Sort array by date value (descending)
message.sortJSONArrayByDate(
"/message/people", // array path
"/dob", // date sort key
true // descending
);
var firstAfterDesc = message.getNode("/message/people[1]/name"); // "Bob"
var lastAfterDesc = message.getNode("/message/people[3]/name"); // "Chris"