# Descending Sort using Standard Ascending Sort Function [closed]

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

## closed as off-topic by Chris Jester-YoungDec 29 '13 at 18:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "General programming questions are off-topic here, but can be asked on Stack Overflow." – Chris Jester-Young
If this question can be reworded to fit the rules in the help center, please edit the question.

• reverse . sort – Thomas Eding Aug 26 '11 at 19:13
• -1 There's no challenge or puzzle here at all. – Peter Taylor Aug 26 '11 at 19:25
• Maybe we should introduce a "rosetta stone" tag, and have the goal be to solve the problem in a variety of languages. – Joey Adams Aug 27 '11 at 1:04
• C++: std::sort(array.rbegin(), array.rend()) – Toby Speight Aug 7 '18 at 13:22

sortByDescending cmp = sortBy (flip cmp)


### Example:

import Data.List
import Data.Function
import Data.Maybe
import Data.Time.LocalTime
import Data.Time.Parse      -- requires module strptime

data Record = Record {
name      :: String,
timestamp :: LocalTime
} deriving Show

sortByDescending cmp = sortBy (flip cmp)

sortByTimestampDescending :: [Record] -> [Record]
sortByTimestampDescending = sortByDescending (compare on timestamp)

parseExample :: (String, String) -> Record
parseExample (n,t) = Record n (fst $fromJust$ strptime "%Y-%m-%d %H:%M:%S" t)

examples :: [Record]
examples = map parseExample
[("eventA", "2011-08-27 15:34:00")
,("eventB", "2011-08-28 13:31:00")
,("eventC", "2011-08-29 10:34:00")
,("eventD", "2011-08-30 16:36:00")]

main = mapM_ print \$ sortByTimestampDescending examples


### Output:

Record {name = "eventD", timestamp = 2011-08-30 16:36:00}
Record {name = "eventC", timestamp = 2011-08-29 10:34:00}
Record {name = "eventB", timestamp = 2011-08-28 13:31:00}
Record {name = "eventA", timestamp = 2011-08-27 15:34:00}


## D

retro(sort!"a.timestamp<b.timestamp"(array));


or

retro(schwartzSort!"a.timestamp"(array));


(you need to import std.algorithm and std.range)

though honestly reversing an array without a build-in function is simple as it is

int i=0,j=arr.length;while(i<--j)swap(arr[i++],arr[j]);


or without a swap function:

int i=0,j=arr.length;while(i<--j){auto tmp=arr[i];arr[i++]=arr[j];arr[j]=tmp;}


### Scala:

val src = """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'"""

class NT (name: String, val n: String, time: String, val ts: String)
val nts = src.split ("[,:] ").sliding (4, 4).map (l =>
new NT (l(0), l(1), l(2), l(3))).toList
nts.sortWith ((a, b) => a.ts < b.ts).reverse.foreach (k =>
println (k.ts + " " + k.n))


Result:

'2011-08-30 16:36:00' 'eventD'
'2011-08-29 10:34:00' 'eventC'
'2011-08-28 13:31:00' 'eventB'
'2011-08-27 15:34:00' 'eventA'