Changed the foreach to for statements, if an empty string is passed to the sort function the value will be considered found