function sort_index(data_dir,sparse_file,mode_slice_dir,mode)
%SORT_INDEX sorts a sparse tensor index file along the given mode
%
%   Peter Turney
%   October 20, 2007
%
%   Copyright 2007, National Research Council of Canada
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see http://www.gnu.org/licenses/.
%
%%  sort the index
%
fprintf('SORT_INDEX is running ...\n');
%
%   file names
%
input_file  = [data_dir, '/', sparse_file];
sub_dir     = [data_dir, '/', mode_slice_dir];
sorted_file = [sub_dir, '/', 'sorted.txt'];
%
%   call Unix 'sort' command
%
%   -n = numerical sorting
%   -k = key to sort on
%   -s = stable sorting
%   -S = memory for sorting buffer
%   -o = output file
%
%   - the 'sort' command is a standard part of Unix and Linux
%   - if you are running Windows, you can get 'sort' by
%     installing Cygwin
%   - the sort buffer is set here to 1 GiB; you can set it
%     to some other value, based on how much RAM you have
%
command = sprintf('sort -n -s -S 1G -k %d,%d -o %s %s', ...
    mode, mode, sorted_file, input_file);
%
fprintf('   calling Unix sort for mode %d\n', mode);
unix(command);
%
fprintf('SORT_INDEX is done\n');
%