diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c
index 416fc4e240..6a992ff184 100644
--- a/src/backend/optimizer/path/pathkeys.c
+++ b/src/backend/optimizer/path/pathkeys.c
@@ -408,9 +408,9 @@ group_keys_reorder_by_pathkeys(List *pathkeys, List **group_pathkeys,
 		 * Give up if we can't find the matching pointer.  Also give up if
 		 * there is no sortclause reference for some reason.
 		 */
-		if (foreach_current_index(lc) >= num_groupby_pathkeys ||
-			!list_member_ptr(grouping_pathkeys, pathkey) ||
-			pathkey->pk_eclass->ec_sortref == 0)
+		if (pathkey->pk_eclass->ec_sortref == 0 ||
+			foreach_current_index(lc) >= num_groupby_pathkeys ||
+			!list_member_ptr(grouping_pathkeys, pathkey))
 			break;
 
 		/*
