Skip to main content
`Ctrl`+`K`

# skbio.table.Table.sort

Table.sort(sort_f=<function natsort>, axis='sample')

  Return a table sorted along axis

  Parameters:
    sort_f function, optional

      Defaults to `biom.util.natsort`. A function that takes a list of values and sorts it

    axis{‘sample’, ‘observation’}, optional

      The axis to operate on

  Returns:
    biom.Table

      A table whose samples or observations are sorted according to the sort_f function

  Examples

      >>> import numpy as np
      >>> from biom.table import Table

  Create a 2x3 BIOM table:

      >>> data = np.asarray([[1, 0, 4], [1, 3, 0]])
      >>> table = Table(data, ['O2', 'O1'], ['S2', 'S1', 'S3'])
      >>> print(table)
      # Constructed from biom file
      #OTU ID S2  S1  S3
      O2  1.0 0.0 4.0
      O1  1.0 3.0 0.0

  Sort the order of samples in the table using the default natural sorting:

      >>> new_table = table.sort()
      >>> print(new_table)
      # Constructed from biom file
      #OTU ID S1  S2  S3
      O2  0.0 1.0 4.0
      O1  3.0 1.0 0.0

  Sort the order of observations in the table using the default natural sorting:

      >>> new_table = table.sort(axis='observation')
      >>> print(new_table)
      # Constructed from biom file
      #OTU ID S2  S1  S3
      O1  1.0 3.0 0.0
      O2  1.0 0.0 4.0

  Sort the samples in reverse order using a custom sort function:

      >>> sort_f = lambda x: list(sorted(x, reverse=True))
      >>> new_table = table.sort(sort_f=sort_f)
      >>> print(new_table)
      # Constructed from biom file
      #OTU ID S3  S2  S1
      O2  4.0 1.0 0.0
      O1  0.0 1.0 3.0