// Sort by Gender (Females before Males) then LastName ascending private static bool Output1CompareFunc(Person person1, Person person2) { if (person1.Gender > person2.Gender) { return true; } if (person1.Gender < person2.Gender) { return false; } return string.CompareOrdinal(person1.LastName, person2.LastName) < 0; }
private Person ParseByComma(string record) { Person person = null; var fields = record.Split(','); if (!fields.Any()) { return null; } Gender gender; if (fields.Length < 3 || !Enum.TryParse(fields[2], out gender)) { gender = Gender.Female; } DateTime dateOfBirth; if (fields.Length < 5 || !DateTime.TryParseExact(fields[4], "M/d/yyyy", _culture, DateTimeStyles.AssumeUniversal, out dateOfBirth)) { dateOfBirth = DateTime.MinValue; } person = new Person { LastName = fields[0], FirstName = fields.Length > 1 ? fields[1] : string.Empty, Gender = gender, FavoriteColor = fields.Length > 3 ? fields[3] : string.Empty, DateOfBirth = dateOfBirth }; return person; }
public bool CustomCompare(Person person, Func<Person, Person, bool> compareFunc) { return compareFunc(this, person); }
private static bool Output3CompareFunc(Person person1, Person person2) { return string.CompareOrdinal(person1.LastName, person2.LastName) >= 0; }
private static bool Output2CompareFunc(Person person1, Person person2) { return person1.DateOfBirth < person2.DateOfBirth; }