{"record":{"id":"8c3252b63f74bae4","repo":"stride3d/stride","slug":"the-type-of-this-collection-does-not-implement-any","errorCode":null,"errorMessage":"The type of this collection does not implement any IComparable interface and no IComparer has been provided","messagePattern":"The type of this collection does not implement any IComparable interface and no IComparer has been provided","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs","lineNumber":84,"sourceCode":"            {\n                DefaultCompareFunc = (item1, item2) => ((IComparable)item1!).CompareTo(item2);\n            }\n            else\n            {\n                DefaultCompareFunc = (item1, item2) =>\n                {\n                    if (item1 is null && item2 is null)\n                        return 0;\n\n                    return item1 is not null ? ((IComparable)item1).CompareTo(item2) : -((IComparable)item2!).CompareTo(item1);\n                };\n            }\n        }\n\n        // If we have no comparer at this point we're dead\n        if (DefaultCompareFunc is null)\n        {\n            throw new ArgumentException(\"The type of this collection does not implement any IComparable interface and no IComparer has been provided\");\n        }\n    }\n\n    /// <summary>\n    /// Seach the index of an item.\n    /// </summary>\n    /// <param name=\"item\">The item to search.</param>\n    /// <returns>The index of the item in the collection, or -1 if the item does not exist in the collection.</returns>\n    public int BinarySearch(T item)\n    {\n        return GetIndex(item, false);\n    }\n\n    /// <summary>\n    /// Searches the index of key in the collection, using a provided function to compare items to the key.\n    /// </summary>\n    /// <typeparam name=\"TSearch\">The type of the key to search.</typeparam>\n    /// <param name=\"key\">The key to search in the collection.</param>","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation/Collections/SortedObservableCollection.cs#L66-L102","documentation":"SortedObservableCollection must know how to order its items. At construction, it builds DefaultCompareFunc from a supplied IComparer, a comparer type, or T's IComparable implementation. If none is available it throws ArgumentException, since a sorted collection without comparison cannot insert items.","triggerScenarios":"Constructing SortedObservableCollection<T> where T does not implement IComparable and no comparer is passed; passing a null comparer; specifying a comparer type that cannot be instantiated.","commonSituations":"Using plain POCO/view-model classes without IComparable as items; refactoring removed an IComparable implementation; forgetting the comparer constructor argument after changing T.","solutions":["Pass an IComparer<T> (or comparison lambda, per constructor overloads) to the constructor","Implement IComparable<T> on the item type T","Use a constructor overload accepting a Comparison<T> delegate","Verify T didn't change to a non-comparable type after a refactor"],"exampleFix":"// before\nvar col = new SortedObservableCollection<MyVm>();\n// after\nvar col = new SortedObservableCollection<MyVm>(Comparer<MyVm>.Create((a, b) => a.Name.CompareTo(b.Name)));","handlingStrategy":"validation","validationCode":"if (typeof(IComparable<MyVm>).IsAssignableFrom(typeof(MyVm)) || comparer != null) { /* safe to construct */ }","typeGuard":null,"tryCatchPattern":"try { var col = new SortedObservableCollection<MyVm>(); } catch (ArgumentException) { var col = new SortedObservableCollection<MyVm>(defaultComparer); }","preventionTips":["Always supply an explicit comparer in the constructor for clarity","Implement IComparable<T> on item types intended for sorted collections","Re-check comparers after refactoring item types"],"tags":["collections","sorting","argument"],"backgroundTag":"missing-required-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}