A5.u.arrayorder Method
Syntax
Arguments
- arrayarray
The array to order items in.
- orderstringarrayfunction
The order operation(s). If more then one order operation wants to be done, an array of orders should be passed in.
Description
Order the items in the array.
Discussion
The A5.u.array.order method allows for the ordering of an array of objects. A string, or array of strings can be passed in. The first part of the string should be the name of the property in each item in the array to order on.
A colon ":" can be used to add further operations for the order on the given property name. Supported operations are "descending", "length", "number" and "date".
The "descending" operation (which can also be shortened to "desc" or "d") is used to reverse the direction that the property is ordered on. For example "CustomerName:desc" would order the array by the "CustomerName" property in descending order.
The "length" operation will return the length of the value in item. For example "Memo:length" would order the "Memo" property by the length of the text in each entry in the array.
The "number" operation will turn a string into a number so that it sorts correctly. For example "Price:number" would order the "Price" property as if it was a number.
The "date" operation will convert a string format date into an actual date. To specify a format for the date, the format is placed in parentheses. For example "DOB:date(MM/dd/yyyy)" would order the "DOB" property using the date format "MM/dd/yyyy".
Multiple operations can be chained together by adding addition colons ":". For example "DOB:date(MM/dd/yyyy):desc" for order the "DOB" property as a date in descending order.
Example
var arr = [{cat: 'a', num: 3},{cat: 'cat', num: 2},{cat: 'b', num: 5},{cat: 'fred', num: 2},{cat: 'z', num: 3},{cat: 'a', num: 1}]; A5.u.array.order(arr,['cat:length','num:desc']); // arr = [ // {"cat":"b","num":5}, // {"cat":"a","num":3}, // {"cat":"z","num":3}, // {"cat":"a","num":1}, // {"cat":"cat","num":2}, // {"cat":"fred","num":2} // ]