EllanJiang/GameFramework · error · GameFrameworkException

Comparison is invalid.

Error message

Comparison is invalid.

What it means

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.

Solutions

  1. Provide a valid comparison such as `(a, b) => a.Id.CompareTo(b.Id)`
  2. Use the non-sorting GetDataRows(Predicate<T>) overload when sorting is not needed
  3. Null-check or default the comparison at the call site: `cmp ??= (a,b) => 0;`
  4. Catch GameFrameworkException if the comparison arrives from user configuration

Example fix

// before
table.GetDataRows(sortCmp); // sortCmp null before settings load
// after
table.GetDataRows(sortCmp ?? ((a, b) => a.Id.CompareTo(b.Id)));
Defensive patterns

Strategy: validation

Validate before calling

if (comparison == null) comparison = (a, b) => a.Id.CompareTo(b.Id);
T[] rows = dataTable.GetDataRows(comparison);

Type guard

static bool Sortable<T>(Comparison<T> c) => c != null;

Try / catch

try { rows = dataTable.GetDataRows(comparison); }
catch (GameFrameworkException ex) when (ex.Message == "Comparison is invalid.") { rows = Array.Empty<T>(); Log.Error("null comparison"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/116966c4222cf250. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/DataTable/DataTableManager.DataTable.cs:228

                foreach (KeyValuePair<int, T> dataRow in m_DataSet)
                {
                    if (condition(dataRow.Value))
                    {
                        results.Add(dataRow.Value);
                    }
                }
            }

            /// <summary>
            /// 获取排序后的数据表行。
            /// </summary>
            /// <param name="comparison">要排序的条件。</param>
            /// <returns>排序后的数据表行。</returns>
            public T[] GetDataRows(Comparison<T> comparison)
            {
                if (comparison == null)
                {
                    throw new GameFrameworkException("Comparison is invalid.");
                }

                List<T> results = new List<T>();
                foreach (KeyValuePair<int, T> dataRow in m_DataSet)
                {
                    results.Add(dataRow.Value);
                }

                results.Sort(comparison);
                return results.ToArray();
            }

            /// <summary>
            /// 获取排序后的数据表行。
            /// </summary>
            /// <param name="comparison">要排序的条件。</param>
            /// <param name="results">排序后的数据表行。</param>
            public void GetDataRows(Comparison<T> comparison, List<T> results)

View on GitHub (pinned to d0c010b051)