@staticmethod
def write_sorted_long(buf, offset, value):
    """
        Writes a packed sorted long integer starting at the given buffer offset
        and returns the next offset to be written.

        :param buf: the buffer to write to.
        :type buf: bytearray
        :param offset: the offset in the buffer at which to start writing.
        :type offset: int
        :param value: the long integer to be written.
        :type value: int for python 3 and long for python 2
        :returns: the offset past the bytes written.
        :rtype: int

        Values in the inclusive range [-119,120] are stored in a single byte.
        For values outside that range, the first byte stores the number of
        additional bytes. The additional bytes store (value + 119 for negative
        and value - 121 for positive) as an unsigned big endian integer.
        """
    b1 = offset
    offset += 1
    if value < -119:
        """
            If the value < -119, then first adjust the value by adding 119. Then
            the adjusted value is stored as an unsigned big endian integer.
            """
        value += 119
        """
            Store the adjusted value as an unsigned big endian integer. For an
            negative integer, from left to right, the first significant byte is
            the byte which is not equal to 0xFF. Also please note that, because
            the adjusted value is stored in big endian integer, we extract the
            significant byte from left to right.

            In the left to right order, if the first byte of the adjusted value
            is a significant byte, it will be stored in the 2nd byte of the buf.
            Then we will look at the 2nd byte of the adjusted value to see if
            this byte is the significant byte, if yes, this byte will be stored
            in the 3rd byte of the buf, and the like.
            """
        if value | 72057594037927935 != 18446744073709551615:
            buf[offset] = value >> 56 & 255
            offset += 1
        if value | 281474976710655 != 18446744073709551615:
            buf[offset] = value >> 48 & 255
            offset += 1
        if value | 1099511627775 != 18446744073709551615:
            buf[offset] = value >> 40 & 255
            offset += 1
        if value | 4294967295 != 18446744073709551615:
            buf[offset] = value >> 32 & 255
            offset += 1
        if value | 16777215 != 18446744073709551615:
            buf[offset] = value >> 24 & 255
            offset += 1
        if value | 65535 != 18446744073709551615:
            buf[offset] = value >> 16 & 255
            offset += 1
        if value | 255 != 18446744073709551615:
            buf[offset] = value >> 8 & 255
            offset += 1
        buf[offset] = value & 255
        offset += 1
        """
            value_len is the length of the value part stored in buf. Because the
            first byte of buf is used to stored the length, so we need to minus
            one.
            """
        value_len = offset - b1 - 1
        """
            The first byte stores the number of additional bytes. Here we store
            the result of 0x08 - value_len, rather than directly store
            value_len. The reason is to implement nature sort order for
            byte-by-byte comparison.
            """
        buf[b1] = 8 - value_len & 255
    elif value > 120:
        """
            If the value > 120, then first adjust the value by subtracting 119.
            Then the adjusted value is stored as an unsigned big endian integer.
            """
        value -= 121
        """
            Store the adjusted value as an unsigned big endian integer. For a
            positive integer, from left to right, the first significant byte is
            the byte which is not equal to 0x00.

            In the left to right order, if the first byte of the adjusted value
            is a significant byte, it will be stored in the 2nd byte of the buf.
            Then we will look at the 2nd byte of the adjusted value to see if
            this byte is the significant byte, if yes, this byte will be stored
            in the 3rd byte of the buf, and the like.
            """
        if value & 18374686479671623680 != 0:
            buf[offset] = value >> 56 & 255
            offset += 1
        if value & 18446462598732840960 != 0:
            buf[offset] = value >> 48 & 255
            offset += 1
        if value & 18446742974197923840 != 0:
            buf[offset] = value >> 40 & 255
            offset += 1
        if value & 18446744069414584320 != 0:
            buf[offset] = value >> 32 & 255
            offset += 1
        if value & 18446744073692774400 != 0:
            buf[offset] = value >> 24 & 255
            offset += 1
        if value & 18446744073709486080 != 0:
            buf[offset] = value >> 16 & 255
            offset += 1
        if value & 18446744073709551360 != 0:
            buf[offset] = value >> 8 & 255
            offset += 1
        buf[offset] = value & 255
        offset += 1
        """
            value_en is the length of the value part stored in buf. Because the
            first byte of buf is used to stored the length, so we need to minus
            one.
            """
        value_len = offset - b1 - 1
        """
            The first byte stores the number of additional bytes. Here we store
            the result of 0xF7 + value_len, rather than directly store
            value_len. The reason is to implement nature sort order for
            byte-by-byte comparison.
            """
        buf[b1] = 247 + value_len & 255
    else:
        """
            If -119 <= value <= 120, only one byte is needed to store the value.
            The stored value is the original value adds 127.
            """
        buf[b1] = value + 127 & 255
    return offset
