Fully homomorphic encryption (FHE) is a powerful scheme that can be applied to perform computations directly on encrypted data for ensuring the security of cloud computing. Using the concept of aggregate plaintext, this paper explores how to optimize the compare-and-swap operation, commonly used for sorting and searching in cloud computing, for FHE data. The resulting performance is optimized by properly arranging the operand locations, corresponding to the desired plaintext slots, and then scheduling a sequence of fundamental homomorphic operations designed to accomplish the targeted operation. Moreover, the hypercube plaintext slot structure is considered to efficiently manipulate the required shift operation and merge multiple operands in an aggregate plaintext. Analytical results show that the number of homomorphic multiplication and shift operations needed in the proposed compare-and-swap operation are logn + 4 and 2logn + 1, respectively, for n-bit data, which is at least 16 times faster than related works for n = 64. Applying the proposed scheme can not only reduce the size of required FHE data, but also improve the total computation time of the chosen operation.