changeset 26265:01f1e70c80b6

sortrows.m: Allow sorting of mixed numeric/cellstr cell arrays (bug #42523). * sortrows.m: If mixed numeric/cellstr cell array then use cell2mat before calling sort. Add BIST test for bug #42523.
author Guillaume Flandin <guillaume.offline@gmail.com>
date Tue, 18 Dec 2018 22:24:37 -0800
parents 25d3e8e49d5c
children 895f2f609a96
files scripts/general/sortrows.m
diffstat 1 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/sortrows.m	Tue Dec 18 22:04:23 2018 -0800
+++ b/scripts/general/sortrows.m	Tue Dec 18 22:24:37 2018 -0800
@@ -65,7 +65,7 @@
   default_mode = "ascend";
   reverse_mode = "descend";
 
-  if (issparse (A))
+  if (issparse (A) || iscell (A))
     ## FIXME: Eliminate this case once __sort_rows_idx__ is fixed to
     ##        handle sparse matrices.
     if (nargin == 1)
@@ -97,7 +97,7 @@
     indices = [1:columns(m)]';
     mode(1:columns(m)) = {default_mode};
   else
-    for j = 1:length (c);
+    for j = 1:length (c)
       if (c(j) < 0)
         mode{j} = reverse_mode;
       else
@@ -115,7 +115,11 @@
   mode = flipud (mode');
   i = [1:rows(m)]';
   for j = 1:length (indices);
-    [~, idx] = sort (m(i, indices(j)), mode{j});
+    M = m(i, indices(j));
+    if (iscell (M) && ! iscellstr (M))
+      M = cell2mat (M);
+    endif
+    [~, idx] = sort (M, mode{j});
     i = i(idx);
   endfor
 
@@ -144,6 +148,13 @@
 %! assert (x, full (sx));
 %! assert (idx, sidx);
 
+%!test <*42523>
+%! C = {1, 2, "filename1";
+%!      3, 4, "filename2";
+%!      5, 6, "filename3"};
+%! C2 = sortrows (C, -1);
+%! assert (C2, flipud (C));
+
 ## Test input validation
 %!error sortrows ()
 %!error sortrows (1, 2, 3)