I have this custom IComparer
I use to sort a list of SurveyResponse
objects, but it seems really inefficient, both in terms of runtime performance, and code elegance and maintainability. I thought of implementing IComparable
for each of these components, and each CompareTo
knowing its "parent" to compare, but the sort order for this report I'm making won't necessarily be the same as other sort orders, so that may not work.
It looks like the sort of problem that should be solvable using recursion, but maybe I'm just over-analyzing it?
Are there any best practices that would make this faster and more maintainable?
private class CommentComparer : IComparer<SurveyResponse>
{
public int Compare(SurveyResponse x, SurveyResponse y)
{
// Sort by Application Name...
int result = x.Question.Survey.Feature.Application.Name.CompareTo(
y.Question.Survey.Feature.Application.Name);
if (result != 0) { return result; }
// ...then Feature Name...
result = x.Question.Survey.Feature.Name.CompareTo(
y.Question.Survey.Feature.Name);
if (result != 0) { return result; }
// ...then SurveyTime...
result = x.Question.Survey.TimeTaken.CompareTo(y.Question.Survey.TimeTaken);
if (result != 0) { return result; }
// ...then ID...
result = x.Question.Survey.ID.CompareTo(y.Question.Survey.ID);
if (result != 0) { return result; }
// ...then Position.
return x.Question.Position.CompareTo(y.Question.Position);
}
}