added proper sorting instead of sorting via replace