add final sorting functionality