import torch


def get_sorted_order(lengths):
    """Sorts based on the lengths.

  Args:
    lengths: Lengths to perform sorting on.

  Returns:
    sorted_len: Lengths sorted according to descending order.
    fwd_order: Forward order of the sorting.
    bwd_order: Backward order of the sorting.
  """
    sorted_len, fwd_order = torch.sort(lengths, dim=0, descending=True)
    _, bwd_order = torch.sort(fwd_order)
    return sorted_len, fwd_order, bwd_order