Util function for the sort function