#!/usr/local/bin/perl

# This file is: order_visitors.pl - sort entries in the visitors file by date
# for the DIMACS Info Service
# GEA 9/23/92

# What this program does:

# We wish to sort the Visitors file by visitor arrival date.
# Items looks like this:

# 05/01/93 - 00/00/00 Gerard Cornuejols, Carnegie Mellon Univ. Seymour

# So we'll be sorting on the first date field, which should be of the form mm/dd/yy.


undef @items;

while(<stdin>)    
{
    next unless $_;		# skip blank lines
    push(items, $_);	
}

# This sort is not terribly obvious.  See the Perl book, pp 245-246.

&evaluate_dates;		# turn mm/dd/yy into a numerical value
@sortdata = @items[sort by_date $[..$#items]; # sort the items
print @sortdata;		# output

sub by_date {  $keys[$a] <=> $keys[$b] } # simple numerical comparison
			      
sub evaluate_dates {
# We cannot compare dates lexicographically, so we must do it "numerically".
# To convert mm/dd/yy into a number, we simply consider the date to be
# a quantity of days since day 0.  We just pretend there are 31 days in each
# month, and 365 days in each year.  Therefore # days = 
# day + (month * 31) + (year * 365).

    undef @keys;

    foreach $string (@items)
    {
	$_ = $string;
	local($month, $day, $year) = /\s*(\d\d?)\/(\d\d?)\/(\d\d)/;
	local($value) = $day + ($month * 31) + ($year * 365);
	push(@keys, $value);
    }
}
