Added card sorting function