1 function [resu, idx] = sort(df, varargin)
4 %# @deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})
5 %# @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim})
6 %# @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{mode})
7 %# @deftypefnx {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x}, @var{dim}, @var{mode})
8 %# Return a copy of @var{x} with the elements arranged in increasing
9 %# order. For matrices, @code{sort} orders the elements in each column.
15 %# sort ([1, 2; 2, 3; 3, 1])
23 %# The @code{sort} function may also be used to produce a matrix
24 %# containing the original row indices of the elements in the sorted
25 %# matrix. For example:
29 %# [s, i] = sort ([1, 2; 2, 3; 3, 1])
39 %# If the optional argument @var{dim} is given, then the matrix is sorted
40 %# along the dimension defined by @var{dim}. The optional argument @code{mode}
41 %# defines the order in which the values will be sorted. Valid values of
42 %# @code{mode} are `ascend' or `descend'.
44 %# For equal elements, the indices are such that the equal elements are listed
45 %# in the order that appeared in the original list.
47 %# The @code{sort} function may also be used to sort strings and cell arrays
48 %# of strings, in which case the dictionary order of the strings is used.
50 %# The algorithm used in @code{sort} is optimized for the sorting of partially
54 %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
56 %% This file is part of Octave.
58 %% Octave is free software; you can redistribute it and/or
59 %% modify it under the terms of the GNU General Public
60 %% License as published by the Free Software Foundation;
61 %% either version 2, or (at your option) any later version.
63 %% Octave is distributed in the hope that it will be useful,
64 %% but WITHOUT ANY WARRANTY; without even the implied
65 %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
66 %% PURPOSE. See the GNU General Public License for more
69 %% You should have received a copy of the GNU General Public
70 %% License along with Octave; see the file COPYING. If not,
71 %% write to the Free Software Foundation, 51 Franklin Street -
72 %% Fifth Floor, Boston, MA 02110-1301, USA.
75 %# $Id: sort.m 9585 2012-02-05 15:32:46Z cdemills $
78 if !isa(df, 'dataframe'),
85 indi = 1; while indi <= length(varargin)
86 if isnumeric(varargin{indi}),
88 print_usage('@dataframe/sort');
93 if 3 == dim, vout(indi) = 2; endif
97 print_usage('@dataframe/sort');
101 sort = varargin{indi};
107 if isempty(dim), dim = 1; endif;
114 for indi = 1:resu._cnt(2),
115 [resu._data{indi}, idx(:, indi, :)] = sort\
116 (resu._data{indi}(:, resu._rep{indi}), varargin{:});
117 resu._data{indi} = squeeze(resu._data{indi});
118 resu._rep{indi} = 1:size(resu._data{indi}, 2);
120 if (all([1 == size(idx, 2) 1 == size(idx, 3)])),
121 if (size(resu._ridx, 1) == resu._cnt(1)),
122 resu._ridx = resu._ridx(idx, :);
124 if (!isempty(resu._name{1, 1})),
125 resu._name{1, 1} = resu._name{1, 1}(idx);
126 resu._over{1, 1} = resu._over{1, 1}(idx);
131 resu._name{1, 1} = []; resu._over{1, 1} = [];
135 error('Operation not implemented');
137 for indi = 1:resu._cnt(2),
138 [resu._data{1, indi}, idx(:, indi)] = sort(resu._data{1, indi}, vout(:));
141 error("Invalid dimension %d", dim);
145 if (any(strmatch('quantile', {dummy.name}))),
146 resu = df_whole(resu);
148 resu = dataframe(resu);