public int Compare(string x, string y) { var properties = _entityType.FindPrimaryKey()?.Properties.Select(p => p.Name).ToList(); var xIndex = -1; var yIndex = -1; if (properties != null) { xIndex = properties.IndexOf(x); yIndex = properties.IndexOf(y); } // Neither property is part of the Primary Key // Compare the property names if (xIndex == -1 && yIndex == -1) { return(StringComparer.Ordinal.Compare(x, y)); } // Both properties are part of the Primary Key // Compare the indices if (xIndex > -1 && yIndex > -1) { return(xIndex - yIndex); } // One property is part of the Primary Key // The primary key property is first return((xIndex > yIndex) ? -1 : 1); }