Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2026.0.0 - English


sortrows

Sort rows of a vector, matrix, table, or timeseries

Syntax

out = sortrows(t [, vars, order])
[out, idx] = sortrows(t [, vars, order])

Arguments

t

Input argument, one of:

  • vector, matrix of doubles, booleans, integers, strings, duration or datetime
  • table object
  • timeseries object

vars

Depending on the type of t

  • vector of integers if t is vector or matrix.
  • vector of integers, booleans or strings or cell if t is a table or timeseries. "RowNames" can be used to sort the rows of the table.
Specifies the columns used for sorting. vars can contain negative integers to sort in descending order and will be account only if order is not set.

order

string or cell array of strings. Possible values:

  • "i" (default value) or "ascend": sort in ascending order
  • "d" or "descend": sort in descending order
  • ["i", "d"] or {"i", "d"}: specify order individually for each column

out

vector, matrix, table or timeseries, same type as t, with rows reordered according to the sorting rules.

idx

column vector of integer indices such that out = t(idx, :). idx provides the permutation of the original rows that produces the sorted result.

Description

sortrows sorts the rows of the input data t according to the values in one or several columns.

The argument vars can be specified by indices, variable names or boolean vector, or a cell containing the variable names. When vars is a vector, sorting is applied lexicographically.

The optional argument order allows specifying ascending or descending order globally, or individually for each column specified in vars.

Examples

Sort rows of matrix of doubles

A = [3 7 2; 1 9 4; 2 5 6]
[B, idx] = sortrows(A)
C = sortrows(A, 2, "d")
D = sortrows(A, -2)
C == D
E = sortrows(A, [1 3], ["i", "d"]) // or sortrows(A, [1 -3])
-> [B, idx] = sortrows(A)

 B = [3x3 double]

   1.   9.   4.
   2.   5.   6.
   3.   7.   2.

 idx = [3x1 double]

   2.
   3.
   1.

--> C = sortrows(A, 2, "d")

 C = [3x3 double]

   1.   9.   4.
   3.   7.   2.
   2.   5.   6.

--> D = sortrows(A, -2)

 D = [3x3 double]

   1.   9.   4.
   3.   7.   2.
   2.   5.   6.

--> C == D

 ans = [3x3 boolean]

  T T T
  T T T
  T T T

--> E = sortrows(A, [1 3], ["i", "d"]) // or sortrows(A, [1 -3])

 E = [3x3 double]

   1.   9.   4.
   2.   5.   6.
   3.   7.   2.

Sort rows of matrix of strings

A = ["b" "b"; "a" "y"; "c" "r"]
[B, idx] = sortrows(A, 2, "d")
--> [B, idx] = sortrows(A, 2, "d")

 B = [3x2 string]

  "a"  "y"
  "c"  "r"
  "b"  "b"

 idx = [3x1 double]

   2.
   3.
   1.

Sort rows of table

t = table(["a"; "b"; "c"; "b"], [1; 2; 1; 1], [1; 2; 3; 4])
[t2, idx] = sortrows(t)
--> t = table(["a"; "b"; "c"; "b"], [1; 2; 1; 1], [1; 2; 3; 4])

 t = [4x3 table]

   Var1   Var2   Var3
   ____   ____   ____
                     
   a      1      1   
   b      2      2   
   c      1      3   
   b      1      4   

--> [t2, idx] = sortrows(t)

 t2 = [4x3 table]

   Var1   Var2   Var3
   ____   ____   ____
                     
   a      1      1   
   b      1      4   
   b      2      2   
   c      1      3   

 idx = [4x1 double]

   1.
   4.
   2.
   3.
A = table(["Oak"; "Pine"; "Maple"; "Apple"; "Pear"], [15; 30; 25; 3; 2], ["Tree"; "Tree"; "Tree"; "FruitTree"; "FruitTree"], ..
'VariableNames', ["Name","Height","Type"])
[B, idx] = sortrows(A, 2)
[C, idxC] = sortrows(A, "Height", "d") // sortrows(A, -2) 
[D, idxD] = sortrows(A, [%f %t %f], "i")
--> A = table(["Oak"; "Pine"; "Maple"; "Apple"; "Pear"], [15; 30; 25; 3; 2], ["Tree"; "Tree"; "Tree"; "FruitTree"; "FruitTree"], ..
  >             'VariableNames', ["Name","Height","Type"])

 A = [5x3 table]

   Name    Height     Type   
   _____   ______   _________
                             
   Oak     15       Tree     
   Pine    30       Tree     
   Maple   25       Tree     
   Apple   3        FruitTree
   Pear    2        FruitTree

--> [B, idx] = sortrows(A, 2)

 B = [5x3 table]

   Name    Height     Type   
   _____   ______   _________
                             
   Pear    2        FruitTree
   Apple   3        FruitTree
   Oak     15       Tree     
   Maple   25       Tree     
   Pine    30       Tree     

 idx = [5x1 double]

   5.
   4.
   1.
   3.
   2.

--> [C, idxC] = sortrows(A, "Height", "d") // sortrows(A, -2) 

 C = [5x3 table]

   Name    Height     Type   
   _____   ______   _________
                             
   Pine    30       Tree     
   Maple   25       Tree     
   Oak     15       Tree     
   Apple   3        FruitTree
   Pear    2        FruitTree

 idxC = [5x1 double]

   2.
   3.
   1.
   4.
   5.

--> [D, idxD] = sortrows(A, [%f %t %f], "i")

 D = [5x3 table]

   Name    Height     Type   
   _____   ______   _________
                             
   Pear    2        FruitTree
   Apple   3        FruitTree
   Oak     15       Tree     
   Maple   25       Tree     
   Pine    30       Tree     

 idxD = [5x1 double]

   5.
   4.
   1.
   3.
   2.
Planet = ["Mercury"; "Venus"; "Earth"; "Mars"; "Jupiter"; ...
"Saturn"; "Uranus"; "Neptune"; "Pluto"];

// Type (Pluto included as "Dwarf")
Category = ["Terrestrial"; "Terrestrial"; "Terrestrial"; "Terrestrial"; ...
"GasGiant"; "GasGiant"; "IceGiant"; "IceGiant"; "Dwarf"];

// Distance from the Sun in millions of km
Distance_Mkm = [57.9; 108.2; 149.6; 227.9; 778.5; 1434; 2871; 4495; 5906];

// Equatorial diameter in km
Diameter_km = [4879; 12104; 12742; 6779; 139820; 116460; 50724; 49244; 2376];

// Number of known sattelites
Moons = [0; 0; 1; 2; 79; 82; 27; 14; 5];

T = table(Category, Distance_Mkm, Diameter_km, Moons, ...
'RowNames', Planet, "VariableNames", ....
["Category", "Distance_Mkm", "Diameter_km", "Moons"])

// With RowNames
TT = sortrows(T, "RowNames")

// Sort by distance from the Sun (ascend)
[T1, idx] = sortrows(T, 'Distance_Mkm')

// Sort by decreasing diameter
[T2, idx] = sortrows(T, 'Diameter_km', 'd')
-->  T = table(Category, Distance_Mkm, Diameter_km, Moons, ...
  >  'RowNames', Planet, "VariableNames", ....
  >  ["Category", "Distance_Mkm", "Diameter_km", "Moons"])

 T = [9x4 table]

              Category     Distance_Mkm   Diameter_km   Moons
             ___________   ____________   ___________   _____
                                                             
   Mercury   Terrestrial   57.9           4879          0    
     Venus   Terrestrial   108.2          12104         0    
     Earth   Terrestrial   149.6          12742         1    
      Mars   Terrestrial   227.9          6779          2    
   Jupiter   GasGiant      778.5          139820        79   
    Saturn   GasGiant      1434           116460        82   
    Uranus   IceGiant      2871           50724         27   
   Neptune   IceGiant      4495           49244         14   
     Pluto   Dwarf         5906           2376          5    

--> TT = sortrows(T, "RowNames")

 TT = [9x4 table]

              Category     Distance_Mkm   Diameter_km   Moons
             ___________   ____________   ___________   _____
                                                             
     Earth   Terrestrial   149.6          12742         1    
   Jupiter   GasGiant      778.5          139820        79   
      Mars   Terrestrial   227.9          6779          2    
   Mercury   Terrestrial   57.9           4879          0    
   Neptune   IceGiant      4495           49244         14   
     Pluto   Dwarf         5906           2376          5    
    Saturn   GasGiant      1434           116460        82   
    Uranus   IceGiant      2871           50724         27   
     Venus   Terrestrial   108.2          12104         0    


--> [T1, idx] = sortrows(T, 'Distance_Mkm')

 T1 = [9x4 table]

              Category     Distance_Mkm   Diameter_km   Moons
             ___________   ____________   ___________   _____
                                                             
   Mercury   Terrestrial   57.9           4879          0    
     Venus   Terrestrial   108.2          12104         0    
     Earth   Terrestrial   149.6          12742         1    
      Mars   Terrestrial   227.9          6779          2    
   Jupiter   GasGiant      778.5          139820        79   
    Saturn   GasGiant      1434           116460        82   
    Uranus   IceGiant      2871           50724         27   
   Neptune   IceGiant      4495           49244         14   
     Pluto   Dwarf         5906           2376          5    

 idx = [9x1 double]

   1.
   2.
   3.
   4.
   5.
   6.
   7.
   8.
   9.

--> [T2, idx] = sortrows(T, 'Diameter_km', 'd')

 T2 = [9x4 table]

              Category     Distance_Mkm   Diameter_km   Moons
             ___________   ____________   ___________   _____
                                                             
   Jupiter   GasGiant      778.5          139820        79   
    Saturn   GasGiant      1434           116460        82   
    Uranus   IceGiant      2871           50724         27   
   Neptune   IceGiant      4495           49244         14   
     Earth   Terrestrial   149.6          12742         1    
     Venus   Terrestrial   108.2          12104         0    
      Mars   Terrestrial   227.9          6779          2    
   Mercury   Terrestrial   57.9           4879          0    
     Pluto   Dwarf         5906           2376          5    

 idx = [9x1 double]

   5.
   6.
   7.
   8.
   3.
   2.
   4.
   1.
   9.

Sort rows of timeseries

ts = timeseries(hours([10; 5; 8; 12]), (1:4)')
[tss, idx] = sortrows(ts, 1)
ts2 = timeseries(datetime() + seconds([100; 200; 150]), [3; 1; 2])
[tss, idx] = sortrows(ts2, 2, "d")
--> ts = timeseries(hours([10; 5; 8; 12]), (1:4)')

 ts = [4x1 timeseries]

     Time     Var1
   ________   ____
                  
   10:00:00   1   
   05:00:00   2   
   08:00:00   3   
   12:00:00   4   

--> [tss, idx] = sortrows(ts, 1)

 tss = [4x1 timeseries]

     Time     Var1
   ________   ____
                  
   05:00:00   2   
   08:00:00   3   
   10:00:00   1   
   12:00:00   4   

 idx = [4x1 double]

   2.
   3.
   1.
   4.

--> ts2 = timeseries(datetime() + seconds([100; 200; 150]), [3; 1; 2])

 ts2 = [3x1 timeseries]

          Time           Var1
   ___________________   ____
                             
   2025-09-30 11:39:39   3   
   2025-09-30 11:41:19   1   
   2025-09-30 11:40:29   2   

--> [tss, idx] = sortrows(ts2, 2, "d")

 tss = [3x1 timeseries]

          Time           Var1
   ___________________   ____
                             
   2025-09-30 11:39:39   3   
   2025-09-30 11:40:29   2   
   2025-09-30 11:41:19   1   

 idx = [3x1 double]

   1.
   3.
   2.

See also

  • gsort — sorts boolean, numerical and string arrays

History

VersionDescription
2026.0.0 Function added.
Report an issue
<< members Search and sort vectorfind >>

Copyright (c) 2022-2025 (Dassault Systèmes S.E.)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 16 09:02:30 CEST 2025