@staticmethod
def read_sorted_int(buf, offset):
    """
        Reads a sorted packed integer at the given buffer offset and returns it.

        :param buf: the buffer to read from.
        :type buf: bytearray
        :param offset: the offset in the buffer at which to start reading.
        :type offset: int
        :returns: the integer that was read.
        :rtype: int
        """
    b1 = buf[offset] & 255
    offset += 1
    if b1 < 8:
        byte_len = 8 - b1
        negative = True
    elif b1 > 247:
        byte_len = b1 - 247
        negative = False
    else:
        return b1 - 127
    """
        The following bytes on the buf store the value as a big endian integer.
        We extract the significant bytes from the buf and put them into the
        value in big endian order.
        """
    if negative:
        value = -1
    else:
        value = 0
    if byte_len > 3:
        value = value << 8 | buf[offset] & 255
        offset += 1
    if byte_len > 2:
        value = value << 8 | buf[offset] & 255
        offset += 1
    if byte_len > 1:
        value = value << 8 | buf[offset] & 255
        offset += 1
    value = value << 8 | buf[offset] & 255
    offset += 1
    """
        After get the adjusted value, we have to adjust it back to the original
        value.
        """
    if negative:
        value -= 119
    else:
        value += 121
    return value
