-5
\$\begingroup\$

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

\$\endgroup\$

closed as off-topic by Chris Jester-Young Dec 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.

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

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}
\$\endgroup\$
0
\$\begingroup\$

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;}
\$\endgroup\$
0
\$\begingroup\$

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'
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.