Sort items algorithm