Descending Sort using Standard Ascending Sort Function

-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

fksdfsdf

Posted 2011-08-26T17:09:43.533

Reputation: 1

Question was closed 2013-12-29T18:31:54.053

C++: std::sort(array.rbegin(), array.rend()) – Toby Speight – 2018-08-07T13:22:33.723

1reverse . sort – Thomas Eding – 2011-08-26T19:13:36.963

2-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

Answers

4

Haskell

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}

Joey Adams

Posted 2011-08-26T17:09:43.533

Reputation: 9 929

0

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;}

ratchet freak

Posted 2011-08-26T17:09:43.533

Reputation: 1 334

0

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'

user unknown

Posted 2011-08-26T17:09:43.533

Reputation: 4 210