Skip to content

TreeSubSet.Min and TreeSubSet.Max work incorrectly with custom comparer #54834

@petrogavriluk

Description

@petrogavriluk

Description

The code below gives the wrong result:

var comparer = Comparer<int>.Create((x, y) => x - y);  
var set = new SortedSet<int>(new[]{1, 4}, comparer);  
var view = set.GetViewBetween(1, 2);  
Console.WriteLine(view.Max); // Prints 4 instead of 1  

According to the documentation of IComparer.Compare() it can return any integer, not only [-1, 0, 1]. So this comparer should be ok.

Configuration

I run it on .net 5, but I think it is present in other versions as well.

Regression?

A similar issue was mentioned in 20474 (without comparer though). So I think it is regression.

Other information

I have found the reason why it happens in code:


The comparison result is checked against 1 and -1, instead of (comp > 0) and (comp < 0).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions