{"record":{"id":"638180233ef6aea7","repo":"TheAlgorithms/C-Sharp","slug":"new-value-is-not-sorting-sorting-descending-less-greater","errorCode":null,"errorMessage":"New value is not {(sorting != Sorting.Descending ? \"less\" : \"greater\")} than old value.","messagePattern":"New value is not (.+?) than old value\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/PairingHeap/PairingHeap.cs","lineNumber":60,"sourceCode":"        Count--;\n        return minMax.Value;\n    }\n\n    /// <summary>\n    /// Update heap key [O(log(n))].\n    /// </summary>\n    public void UpdateKey(T currentValue, T newValue)\n    {\n        if (!mapping.ContainsKey(currentValue))\n        {\n            throw new ArgumentException(\"Current value is not present in this heap.\");\n        }\n\n        var node = mapping[currentValue]?.Where(x => x.Value.Equals(currentValue)).FirstOrDefault();\n\n        if (comparer.Compare(newValue, node!.Value) > 0)\n        {\n            throw new ArgumentException($\"New value is not {(sorting != Sorting.Descending ? \"less\" : \"greater\")} than old value.\");\n        }\n\n        UpdateNodeValue(currentValue, newValue, node);\n\n        if (node == root)\n        {\n            return;\n        }\n\n        DeleteChild(node);\n\n        root = RebuildHeap(root, node);\n    }\n\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/PairingHeap/PairingHeap.cs#L42-L78","documentation":"PairingHeap.UpdateKey throws ArgumentException when the new value compares as greater than (or equal order-inverting for) the old value relative to the heap's sorting direction. For a min-heap the new key must be less than the old (and greater for a max-heap/descending sort); the heap only supports decrease-key / increase-key in one direction.","triggerScenarios":"Calling UpdateKey(current, newValue) where comparer.Compare(newValue, currentValue) > 0 on a min-heap — i.e. attempting to increase a key in a min-heap, or decrease a key in a max-heap (Sorting.Descending).","commonSituations":"Dijkstra relaxation writing the wrong distance; mixing up min-heap and max-heap usage; changing sort direction without inverting the update logic; passing arguments in the wrong order (currentValue and newValue swapped).","solutions":["For a min-heap, only pass a newValue smaller than the current value (and larger for max-heap)","Verify argument order: UpdateKey(currentValue, newValue), not reversed","Catch ArgumentException if your algorithm legitimately may attempt both directions and skip/re-insert instead"],"exampleFix":"// before\nheap.UpdateKey(node, node.Distance + 5); // increases key in min-heap\n// after\nif (comparer.Compare(node.Distance + 5, node.Distance) < 0)\n    heap.UpdateKey(node, node.Distance + 5);\nelse { heap.Delete(node); heap.Insert(node with distance); } // re-insert instead","handlingStrategy":"validation","validationCode":"if (comparer.Compare(newValue, currentValue) > 0) { /* re-insert instead of UpdateKey */ } else { heap.UpdateKey(currentValue, newValue); }","typeGuard":null,"tryCatchPattern":"try { heap.UpdateKey(currentValue, newValue); } catch (ArgumentException) { /* wrong direction: fall back to delete+insert */ }","preventionTips":["For min-heaps only decrease keys; for max-heaps only increase keys","Double-check currentValue/newValue argument order","If keys can move either way, remove and re-insert instead of UpdateKey"],"tags":["data-structures","heap","argument-exception","decrease-key"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}