Add sorting function to reduce fragmentation