{"ScriptPreparationCode":null,"TestCases":[{"Name":"one","Code":"function associativeArrayDateSort (direction, field) {\r\n return function (a, b) {\r\n\r\n a = new Date(a[ field ]).valueOf()\r\n b = new Date(b[ field ]).valueOf()\r\n\r\n let compare = ( a \u003E b ) - ( a \u003C b )\r\n\r\n if (direction === \u0027DESC\u0027) {\r\n compare *= -1\r\n }\r\n\r\n return compare\r\n }\r\n}\r\n\r\nlet my_array = [\r\n {\r\n field_1: \u0027aaa\u0027,\r\n field_2: \u0027bbb\u0027,\r\n date_field: new Date(\u00272020-03-10 00:15:00\u0027)\r\n },\r\n {\r\n field_1: \u0027ccc\u0027,\r\n field_2: \u0027ddd\u0027,\r\n date_field: new Date(\u00272020-03-10 00:20:00\u0027)\r\n },\r\n {\r\n field_1: \u0027eee\u0027,\r\n field_2: \u0027fff\u0027,\r\n date_field: new Date(\u00272020-03-10 00:05:00\u0027)\r\n },\r\n {\r\n field_1: \u0027ggg\u0027,\r\n field_2: \u0027hhh\u0027,\r\n date_field: new Date(\u00272020-03-10 00:10:00\u0027)\r\n }\r\n]\r\n\r\nmy_array.sort(associativeArrayDateSort(\u0027DESC\u0027, \u0027date_field\u0027))\r\n\r\nconsole.info(my_array)","IsDeferred":false},{"Name":"two","Code":"function associativeArrayDateSort( direction, field ) {\r\n return function(a,b){\r\n if ( new Date( a[ field ] ) === new Date( b[ field ] ) ) {\r\n return 0;\r\n }\r\n\r\n if(direction === \u0022ASC\u0022){\r\n return new Date( a[ field ] ) \u003E new Date( b[ field ] ) ? 1 : -1\r\n }\r\n\r\n return new Date( a[ field ] ) \u003E new Date( b[ field ] ) ? -1 : 1\r\n }\r\n}\r\n\r\n\r\nlet my_array = [\r\n {\r\n field_1: \u0027aaa\u0027,\r\n field_2: \u0027bbb\u0027,\r\n date_field: new Date(\u00272020-03-10 00:15:00\u0027)\r\n },\r\n {\r\n field_1: \u0027ccc\u0027,\r\n field_2: \u0027ddd\u0027,\r\n date_field: new Date(\u00272020-03-10 00:20:00\u0027)\r\n },\r\n {\r\n field_1: \u0027eee\u0027,\r\n field_2: \u0027fff\u0027,\r\n date_field: new Date(\u00272020-03-10 00:05:00\u0027)\r\n },\r\n {\r\n field_1: \u0027ggg\u0027,\r\n field_2: \u0027hhh\u0027,\r\n date_field: new Date(\u00272020-03-10 00:10:00\u0027)\r\n }\r\n]\r\n\r\nmy_array.sort( associativeArrayDateSort(\u0027DESC\u0027, \u0027created_at\u0027) )","IsDeferred":false}]}