-5
Taking an array of records. For example
[
name: 'eventA', timestamp: '2011-08-27 15:34:00',
name: 'eventB', timestamp: '2011-08-28 13:31:00',
name: 'eventC', timestamp: '2011-08-29 10:34:00',
name: 'eventD', timestamp: '2011-08-30 16:36:00',
]
How can I sort these by timestamp in a descending using a standard natural ascending sort algorithm built into most languages. E.g.
E.g
C#: collection.Sort(record => record.timestamp)
Python: sort(collection, lambda record: record.timetstamp)
You can not use the sortByDescending
methods, of course most languages have them (or a flag to sort fn for descending)
Somehow have to transform the record.timestamp
so that it sorts descending
Solution 1: Parse the date into a Date object provided by most languages and get time in milliseconds/seconds since 1970. Minus the amount from 0 to get the negated value and sort by this.
Extra points for no expensive date conversions
C++:
std::sort(array.rbegin(), array.rend())
– Toby Speight – 2018-08-07T13:22:33.7231
reverse . sort
– Thomas Eding – 2011-08-26T19:13:36.9632-1 There's no challenge or puzzle here at all. – Peter Taylor – 2011-08-26T19:25:51.767
Maybe we should introduce a "rosetta stone" tag, and have the goal be to solve the problem in a variety of languages. – Joey Adams – 2011-08-27T01:04:13.207