Chcę posortować listę w C#, przez właściwość obiektów przechowywanych w nim. Mam to:Odbicie get właściwość obiektu do sortowania listy
if (sortColumn == "Login")
{
if (sortDir == "ASC")
{
filteredList.Sort((x, y) => string.Compare(x.Login, y.Login, true));
}
else
{
filteredList.Sort((x, y) => string.Compare(y.Login, x.Login, true));
}
}
i działa dobrze, ale chcę zrobić to bardziej ogólne, żeby nie trzeba znać pola sortowania. Mam na myśli coś takiego:
//With sortColumn = "Login";
if (sortDir == "ASC")
{
filteredList.Sort((x, y) => string.Compare(x.GetType().GetProperty(sortColumn), y.GetType().GetProperty(sortColumn), true));
}
else
{
filteredList.Sort((x, y) => string.Compare(y.GetType().GetProperty(sortColumn), x.GetType().GetProperty(sortColumn), true));
}
Oczywiście to nie działa, ale to, co chcę. Czy to możliwe w jakikolwiek sposób?
Dzięki.
próbowałeś '.... GetProperty (sortColumn) .GetValue (...) '? –