import operator


def rkLeavesByCompressionRatio(hmat):
    """Sort the Rk leaves by descending compression ratio.

    Args:
      hmat: an instance of HMatrixStructure

    Returns:
      [(compression_ratio1, leaf1), ...]
    """
    leaves = hmat.listRkLeaves()
    ratio_leaves = [(x.compressionRatio(), x) for x in leaves]
    ratio_leaves.sort(key=operator.itemgetter(0), reverse=True)
    return ratio_leaves