slandau
slandau

Reputation: 24052

SelectList not sorting at all

public static SelectList HolidayDays()
{
    SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet);

    //sort
    retval.OrderBy(i => i.Text == "New York")
          .ThenBy(i => i.Text == "London")
          .ThenBy(i => i.Text);

    return retval;
}

Why isn't the SelectList sorting at all? It stays in the same exact order that it is in before I even perform the sort operations, but it compiles and runs without error.

Upvotes: 0

Views: 1558

Answers (1)

hunter
hunter

Reputation: 63522

OrderBy and ThenBy return a collection, which you're not setting to your return value;


SelectList retval = GenerateKeyValueList<HolidayCity>(HolidayCityHelper.GetFriendlyName, HolidayCity.NotSet);

return new SelectList(retval
        .OrderByDescending(i => i.Text == "New York")
        .ThenByDescending(i => i.Text == "London")
        .ThenBy(i => i.Text).ToList(), 
    "Value", "Text");

Upvotes: 4

Related Questions