First version of smart sorting