performance improvement of ordering algorithm