I have a dynamic dropdown list that I need to sort alphabetically, but the issue is, there is some text within brackets that appears on top.
I was asked to sort the text in brackets alphabetically (in this case, it begins with an "R" -- they want the "[OLD] ROI" to be placed above "ROI - English"), even though it technically sorts correctly.
Can someone please help me with a solution? I cannot find an answer anywhere, and I have honestly never done this. Thank you.
Here is the view:
<div class="form-inline">
<div class="form-group">
<label for="consentFormSelect">Consent Form : </label>
<select id="consentFormSelect" class="form-control" onchange="onConsentFormSelectChange()">
<option value="">Select An Option</option>
@foreach (var item in Model.ConsentForms.OrderBy(m => m.CodeDesc.ToString()).ToList())
{
<option value="@item.CodeId">@item.CodeDesc</option>
}
</select>
</div>
</div>
Screenshot of the UI:
One way (not the correct one) is to create a property inside your model:
public class ConsentForm
{
...
public string CodeDescParsed
{
get { return CodeDesc.Replace("[", "").Replace("]", ""); }
}
...
}
Then:
@foreach (var item in Model.ConsentForms.OrderBy(m => m.CodeDescParsed.ToString()).ToList())
There are other ways as well, but they require advanced Linq.