{"record":{"id":"116966c4222cf250","repo":"EllanJiang/GameFramework","slug":"comparison-is-invalid","errorCode":null,"errorMessage":"Comparison is invalid.","messagePattern":"Comparison is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/DataTable/DataTableManager.DataTable.cs","lineNumber":228,"sourceCode":"                foreach (KeyValuePair<int, T> dataRow in m_DataSet)\n                {\n                    if (condition(dataRow.Value))\n                    {\n                        results.Add(dataRow.Value);\n                    }\n                }\n            }\n\n            /// <summary>\n            /// 获取排序后的数据表行。\n            /// </summary>\n            /// <param name=\"comparison\">要排序的条件。</param>\n            /// <returns>排序后的数据表行。</returns>\n            public T[] GetDataRows(Comparison<T> comparison)\n            {\n                if (comparison == null)\n                {\n                    throw new GameFrameworkException(\"Comparison is invalid.\");\n                }\n\n                List<T> results = new List<T>();\n                foreach (KeyValuePair<int, T> dataRow in m_DataSet)\n                {\n                    results.Add(dataRow.Value);\n                }\n\n                results.Sort(comparison);\n                return results.ToArray();\n            }\n\n            /// <summary>\n            /// 获取排序后的数据表行。\n            /// </summary>\n            /// <param name=\"comparison\">要排序的条件。</param>\n            /// <param name=\"results\">排序后的数据表行。</param>\n            public void GetDataRows(Comparison<T> comparison, List<T> results)","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/DataTable/DataTableManager.DataTable.cs#L210-L246","documentation":"The sorted array-returning overload GetDataRows(Comparison<T>) requires a non-null comparison delegate used for List<T>.Sort. A null comparison throws GameFrameworkException(\"Comparison is invalid.\") at line 228 before any row is collected.","triggerScenarios":"Calling GetDataRows((Comparison<T>)null); e.g. a sort-key delegate held in a nullable variable that was never set, or passing null when the caller thinks sorting is optional.","commonSituations":"Sort selector stored per-UI-panel and only assigned after the panel config loads; generic query helpers that pass through a caller-supplied Comparison<T> without a default.","solutions":["Provide a valid comparison such as `(a, b) => a.Id.CompareTo(b.Id)`","Use the non-sorting GetDataRows(Predicate<T>) overload when sorting is not needed","Null-check or default the comparison at the call site: `cmp ??= (a,b) => 0;`","Catch GameFrameworkException if the comparison arrives from user configuration"],"exampleFix":"// before\ntable.GetDataRows(sortCmp); // sortCmp null before settings load\n// after\ntable.GetDataRows(sortCmp ?? ((a, b) => a.Id.CompareTo(b.Id)));","handlingStrategy":"validation","validationCode":"if (comparison == null) comparison = (a, b) => a.Id.CompareTo(b.Id);\nT[] rows = dataTable.GetDataRows(comparison);","typeGuard":"static bool Sortable<T>(Comparison<T> c) => c != null;","tryCatchPattern":"try { rows = dataTable.GetDataRows(comparison); }\ncatch (GameFrameworkException ex) when (ex.Message == \"Comparison is invalid.\") { rows = Array.Empty<T>(); Log.Error(\"null comparison\"); }","preventionTips":["Use the non-sorting overload when sorting is not required","Default sort delegates when sort settings are absent","Null-check UI-bound sort selectors before querying"],"tags":["csharp","datatable","sorting","null-argument"],"backgroundTag":"null-argument","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}