Skip to content

MaxBy inconsistent results with tie-breaks #61317

@martingbrown

Description

@martingbrown

Description

When all the items in the list have a null key MaxBy returns the last item in the list. When they all have a key value but the values are the same the first item is returned. To me that seems inconsistent.

Reproduction Steps

using System;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		var l1 = new Tuple<string, string>[]
		{
			new Tuple<string, string>("", "A"),
			new Tuple<string, string>("", "B")
		};
		
		var l2 = new Tuple<string, string>[]
		{
			new Tuple<string, string>(null, "A"),
			new Tuple<string, string>(null, "B")
		};
		
		var r1 = l1.MaxBy(t => t.Item1);
		var r2 = l2.MaxBy(t => t.Item1);
		
		Console.WriteLine(r1.Item2);
		Console.WriteLine(r2.Item2);
	}
}

Expected behavior

The Reproduction code should print AA or BB.

Actual behavior

The reproduction code prints AB

Regression?

I think this is new in .Net 6.

Known Workarounds

Don't use lists with all the keys the same.

Configuration

I got this in .Net 6 RC2. I don't think the OS or CPU are going to make any difference.

Other information

No response

Metadata

Metadata

Labels

area-System.LinqenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions