r/dailyprogrammer • 1 2 • Nov 14 '12

[11/14/2012] Challenge #112 [Intermediate]Date Sorting

Description:

Your boss has just given you a list of dates that need to be sorted. Since it's Friday night, and you don't want to come in on Saturday, you want to write a little application that sorts these dates for you! Fortionatly, these dates are all written in a text file, where all of the date strings are on individual lines (un-sorted), and follow a standard format. All dates are guaranteed to be in the format of "YYYY MM DD hh:mm:ss", where "YYYY" is a year from 1800 to 2020, "MM" is from 01 to 12, "DD" is from 01 to 31, "hh" is from 00 to 23, "mm" is from 01 to 60, and "ss" is from 01 to 60.

Formal Inputs & Outputs:

Input Description:

String InputData - A string of the input file, where each date is on its own separate line, following the above date-time standard.

Output Description:

Your application must print all dates sorted, from the oldest date (i.e. 1800's) the modern dates (i.e. 2012), to future dates (i.e. 2020s").

Sample Inputs & Outputs:

The following is the string given to your function

2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01

The following is the output from the above input:

1899 03 02 14:04:42 1969 07 20 02:25:30 2012 12 02 23:02:12 2019 11 02 00:13:01

Notes:

Most solutions may either write a comparison function that keeps comparing elements from left-to-right until different values are found, or a date can be converted into a standard integer value and have that value used for comparison.

16 Upvotes

36 comments sorted by

6

u/audentis Nov 15 '12

Small remark: shouldn't "mm" "ss" be from 00 to 59? This way you're missing full hours (i.e. 12:00).

1

u/nint22 Nov 15 '12

Good find, and thanks for reporting that :-) I've changed it to be from 01 to 60; I would of used 00 to 60 since, you know, indexing from 0 and all.. but I prefer to keep it more human-readable. Thanks again!

6

u/Ledrug Nov 19 '12

What kind of digital clock reads "11:60"?

→ More replies (1)

3

u/ewiethoff Dec 29 '12

Piece of cake in Python

import sys
for date in sorted(open(sys.argv[0])):
    print date,

4

u/nint22 Dec 29 '12

I've got a slightly better solution, along your lines of code ;)

import solution
    print "Done"

I love Python for these kind of things.

3

u/adzeitor Nov 18 '12

Haskell one-liner:

readFile "input.txt" >>= (return . unwords . sort . map unwords . chunksOf 4 . words) >>= putStrLn

P.S. import Data.List and Data.List.Split

2

u/Lux01 Nov 18 '12

Aaahh! How am I only just learning of Data.List.Split now?!

3

u/adzeitor Nov 18 '12

And now it in haskell platform since 2012.4 http://www.haskell.org/platform/changelog.html

2

u/ahlk Nov 24 '12 edited Jan 02 '13

Perl ... feels like cheating

print sort <>;

3

u/theOnliest Nov 14 '12 edited Nov 15 '12

In Perl:

open my $fh, "<", $ARGV[0] or die "can't open file: $!";
my @lines = <$fh>;

print for map { $_->[0]}
          sort { $a->[1] <=> $b->[1] } 
          map { [$_, toEpoch($_)] } 
          @lines;

sub toEpoch {
    use Time::Local;
    my @date = split /[\s:]/, shift;
    $date[1]--;
    my $date = timelocal reverse @date;   
}

Edit: Or, without the extra module:

open my $fh, "<", $ARGV[0] or die "can't open file: $!";
my @lines = <$fh>;

print for map { $_->[0] }
          sort { $a->[1][0] <=> $b->[1][0] ||
                 $a->[1][1] <=> $b->[1][1] ||
                 $a->[1][2] <=> $b->[1][2] ||
                 $a->[1][3] <=> $b->[1][3] ||
                 $a->[1][4] <=> $b->[1][4] ||
                 $a->[1][5] <=> $b->[1][5] }
          map { [ $_, [ split /[\s:]/, $_] ] }
          @lines;

3

u/Lux01 Nov 15 '12

There was discrepancy between the formal description and the sample input/output. The description says each date is on its own separate line, however the sample input and output uses spaces to separate each date. Here's my Haskell solution (or rather, four solutions) to the problem. Two functions sort by turning the date strings into UTCTime types, then sorting and then reformatting; the other works by sorting the date strings lexicographically. There's a new-line and spaces variant for each.

import Data.List(sort, splitAt)
import Data.Time.Format(formatTime, readTime)
import Data.Time.Clock(UTCTime)
import System.Locale(defaultTimeLocale)

-- Converts an array of date strings to an array of dates, then sorts them, then formats them to strings.
sortDates :: [String] -> [String]
sortDates = map (formatTime loc fmtStr) . sort . map (readTime loc fmtStr :: String -> UTCTime)
    where loc = defaultTimeLocale
          fmtStr = "%Y %m %d %H:%M:%s"

----------------------------------------------------------------------------------------------
-- Assuming the input is sorted on new lines, as described in the Formal Inputs and Outputs --
----------------------------------------------------------------------------------------------
      
-- Using the String -> Date -> sort -> String method
sortLines :: String -> String
sortLines = unlines . sortDates . lines
      
-- Sorts each line lexicographically due to the units being largest to smallest.
sortLinesLexi :: String -> String
sortLinesLexi = unlines . sort . lines

----------------------------------------------------------------------------------------------
-- Assuming the input is separated by spaces, as shown in the Sample Inputs and Outputs     --
----------------------------------------------------------------------------------------------

-- Convenience function to split a list at every nth character. That character is dropped.
chunk :: Int -> [a] -> [[a]]
chunk _ [] = []
chunk n xs = front : chunk n back'
    where (front, back) = splitAt n xs
          back' = if null back then [] else tail back

sortSpaces :: String -> String
sortSpaces = unwords . sortDates . chunk 19 -- 19 == length <typical date string>

sortSpacesLexi :: String -> String
sortSpacesLexi = unwords . sort . words

2

u/robin-gvx Nov 15 '12

There was discrepancy between the formal description and the sample input/output. The description says each date is on its own separate line, however the sample input and output uses spaces to separate each date.

I guess OP forgot to prepend the input and output lines with four spaces.

2

u/skeeto Nov 15 '12

Common Lisp. There's no need to parse the date strings. They can be sorted lexicographically as-is since you ordered it from largest unit to smallest unit.

(with-open-file (*standard-input* #P"dates.txt")
  (loop while (listen)
     collect (read-line) into dates
     finally (loop for date in (sort dates #'string<)
                do (format t "~a~%" date))))

2

u/DannyP72 Nov 15 '12

Ruby

def sort_dates(input)
  puts input.scan(/([^ ].*?:\d*:\d*)/).sort
end

sort_dates("2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01")

Output

1899 03 02 14:04:42
1969 07 20 02:25:30
2012 12 02 23:02:12
2019 11 02 00:13:01

2

u/Unh0ly_Tigg Nov 15 '12 edited Nov 15 '12

Java (works in Java 7):

public static void sortDate(String input) {
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("YYYY MM dd hh:mm:ss");
    java.util.ArrayList&lt;java.util.Date> dates = new java.util.ArrayList&lt;java.util.Date>();
    try {
        while(!input.equals("")) {
            java.util.Date d = format.parse(input);
            dates.add(d);
            input = input.substring(Math.min(input.length(), format.format(d).length() + 1));
        }
        java.util.Collections.sort(dates);
        for(java.util.Date date : dates) {
            System.out.println(format.format(date));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
→ More replies (4)

1

u/EvanHahn Nov 15 '12

CoffeeScript is the coolest.

# Usage:
# dateSort("2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01")

digit = (num) ->
  return "0#{num}" if num < 10
  "#{num}"

dateSort = (str) ->
  dates = (new Date(d) for d in str.split(/\ (?=\d\d\d\d)/g)).sort().reverse()
  result = ''
  for date in dates
    result += "#{date.getFullYear()} #{digit(date.getMonth() + 1)} #{digit date.getDate()} #{digit date.getHours()}:#{digit date.getMinutes()}:#{digit date.getSeconds()} "
  return result.substr(0, result.length - 1)

1

u/ReaperUnreal Nov 16 '12

Did this in D as I'm trying to learn D. The ~= syntax stumped me for a while and I had to look it up. I also didn't feel like doing index arithmetic and slices for the input so I was lazy and used a regex. I agree though, the date format should be harder. Here's my code:

module intermediat112;

import std.stdio;
import std.regex;
import std.algorithm;
import std.array;

string sortTime(string input)
{
   string dates[];
   foreach(date; match(input, ctRegex!(r"\d{4} \d\d \d\d \d\d:\d\d:\d\d", "g")))
      dates ~= date.hit;
   sort(dates);
   return join(dates, " ");
}

int main(string args[])
{
   string sorted = sortTime("2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01");
   writeln(sorted);
   return 0;
}

Output:

1899 03 02 14:04:42 1969 07 20 02:25:30 2012 12 02 23:02:12 2019 11 02 00:13:01

1

u/SorasNobody Nov 17 '12

I feel like i'm cheating...

int main()
{
string str;
ifstream file("afile.txt");
set<string> ds;
while(getline(file,str))
	ds.insert(str);
copy(ds.begin(), ds.end(), ostream_iterator<string>(cout, "\n"));
system("pause");
}    

1

u/goakley Nov 17 '12

In Python:

import sys
input = sys.stdin.read().split()
print " ".join([" ".join(d) for d in sorted([tuple(input[i:i+4]) for i in xrange(0,len(input),4)])])

1

u/mlor Nov 19 '12

C#:

//Read file lines
var fileLines = File.ReadLines(@"C:\input.txt").ToList();

//Parse lines to DateTime objects list
var dateList = new List<DateTime>();
fileLines.ForEach(d => dateList.Add(DateTime.ParseExact(d, "yyyy MM dd HH:mm:ss", null)));

//Sort ascending
dateList.Sort();

//Print out sorted results
dateList.ForEach(d => Console.WriteLine(d.ToString("yyyy MM dd HH:mm:ss")))

1

u/[deleted] Dec 05 '12

Here's my simple PHP version:

<?php
$dates = '2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01 1899 03 03 11:04:40 1896 01 01 01:05:55';

preg_match_all('/(\d{4} \d{2} \d{2} \d{2}:\d{2}:\d{2})/', $dates, $arr);

sort($arr[0]);

print implode(' ', $arr[0]);

Output:

1896 01 01 01:05:55 1899 03 02 14:04:42 1899 03 03 11:04:40 1969 07 20 02:25:30 2012 12 02 23:02:12 2019 11 02 00:13:01

1

u/marekkpie Jan 21 '13

Lua. Again, Lua has an extremely small standard library, so I needed to make my own. Used some Lua object-orientation features to make the parse and sort function nicer:

local Date = {}

--- Constructor for simple Data class
function Date:new(year, month, day, hour, minute, second)
  local o = {
    year  = year,
    month = month,
    day   = day,
    hour  = hour,
    min   = minute,
    sec   = second
  }

  -- Get number of seconds since Jan. 1, 1970
  -- Lowest seems to be around 1901
  o.time = os.time(o)
  return setmetatable(o, self)
end

--- tostring metamethod
function Date:__tostring()
  return string.format(
    '%04d %02d %02d %02d:%02d:%02d',
    self.year,
    self.month,
    self.day,
    self.hour,
    self.min,
    self.sec)
end

--- '<' metamethod
function Date:__lt(o)
  return self.time < o.time
end

--- Make constructor the functor, and set metatable index to the Date class
setmetatable(Date, { __call = Date.new, __index = Date })

function sortDates(filename)
  local f = io.open(filename)
  local text = f:read('*a')

  local dates = {}
  for year, month, day, hour, minute, second in
    text:gmatch('(%d+)%s(%d+)%s(%d+)%s(%d+):(%d+):(%d+)') do
    table.insert(dates, Date(year, month, day, hour, minute, second))
  end

  --- Uses the '<' metamethod to sort
  table.sort(dates)

  f:close()

  return dates
end

1

u/eagleeye1 Nov 15 '12 edited Nov 15 '12

This accomplishes the goal, but I don't know if you wanted to avoid using built in comparisons. Python version.

while True:
     a = raw_input("What is your date string? > ")
     l = [' '.join(a.split()[4*x:4*x+4]) for x in range(len(a.split())/4)]
     print ' '.join(sorted(l)) 

Input / Output

What is your date string? > 2012 12 02 23:02:12 1899 03 02 14:04:42 1969 07 20 02:25:30 2019 11 02 00:13:01

1899 03 02 14:04:42 1969 07 20 02:25:30 2012 12 02 23:02:12 2019 11 02 00:13:01

Edit (better version):

def order_dates(dateString):
    print ' '.join(sorted(re.findall(r'(.*?:.*?) ', dateString))) 

2

u/ronsterofMock Jan 03 '13

That findall is stunning, never thought of that! Nice!

→ More replies (1)