check_order (version 0.0)

You can also look at:
#! /usr/bin/ksh

###########################################################################
#
# 
# SYNTAX: check_order file column
#
# DESCRIPTION: This utility checks if a column of a fits file bintable
# DESCRIPTION: is in increasing order. If it isn't issue an exception and sort
# DESCRIPTION: the file.
#
# HISTORY:
#
# VERSION: 0.0
#
##############################################################################

file=$1
column=$2

$UTIL/log_entry "Checking if column $column in $file is in order"

$FTOOLS/cktime infile=$file \
               colname=$column \
               ckequal=yes >stdout${$}.log 2>stderr${$}.log
code=$?
ordered=$(grep ORDERED stdout${$}.log)
$UTIL/ftool_test cktime $code $0 2 stdout${$}.log stderr${$}.log

if [ -z "$ordered" ]; then
     ###################################
     # file is out of order, so sort it
     ###################################
     $UTIL/exception $0 1  "sorting column $column in $file"

     rm -f out.tmp

     $FTOOLS/fmemsort infile=$file   \
                      outfile=out.tmp  \
                      column=$column \
                      method=insert  \
                      ascend=yes     \
                      copyall=yes    \
                      history=yes    >stdout${$}.log 2>stdout${$}.log

     $UTIL/ftool_test fmemsort $? $0 2 stdout${$}.log stderr${$}.log

     if [ -s $z.tmp ]; then
          mv out.tmp file
     fi
     rm -f out.tmp
   


fi





exit 0