英文:
How can I Sort this MVC Drop-Down list alphabetically but IGNORE brackets?
问题
我有一个动态下拉列表,需要按字母顺序对其进行排序,但问题是,括号内的一些文本会出现在顶部。
我被要求按括号内的文本按字母顺序排序(在这种情况下,以“R”开头 - 他们希望将“[OLD] ROI”放在“ROI - English”之上),尽管从技术上讲它已经正确排序了。
请问有人可以帮我找到解决方案吗?我在任何地方都找不到答案,而且说实话,我从未做过这个。谢谢。
这是视图:
<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>
UI界面的截图:
英文:
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:
答案1
得分: 0
一种方法(不正确的方法)是在您的模型内创建一个属性:
public class ConsentForm
{
// ...
public string CodeDescParsed
{
get { return CodeDesc.Replace("[", "").Replace("]", ""); }
}
// ...
}
然后:
@foreach (var item in Model.ConsentForms.OrderBy(m => m.CodeDescParsed.ToString()).ToList())
还有其他方法,但它们需要高级的 Linq。
英文:
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.
答案2
得分: 0
你可以清理项目(删除前缀),根据清理后的值对其进行排序,然后返回基于清理后的值排序的原始值列表。
void Main()
{
List<string> items = new()
{
"[OLD] ROI English Advance Directive(18 +)",
"Advance Directive(18 +) (Spanish)",
"Clinical Supervision Form ",
"Clinical Supervision Form(Spanish) ",
"Consent for Telehealth Services-English ",
"Consent for Telehealth Services-Spanish ",
"Consent for Treatment and Evaluation ",
"Consent for Treatment and Evaluation(Spanish) ",
"Consumer Handbook Acknowledgement ",
"Contexture ROI English ",
"Contexture ROI Spanish ",
"Intake / Annual Consent Packet ",
"Intake / Annual Consent Packet(Spanish) ",
"ROI - English ",
"ROI - Spanish ",
"SUD Release SUD Release(Spanish)",
};
var sortedItems = CustomSort(items, "[OLD] ");
}
public List<string> CustomSort(List<string> items, string prefix)
{
List<(string cleaned, string original)> cleaned = new();
foreach(var i in items)
cleaned.Add(new(i.Replace(prefix, string.Empty), i));
var sorted = cleaned.OrderBy(i => i.cleaned).ToList();
return sorted.Select(i => i.original).ToList();
}
sortedItems:
- Advance Directive(18 +) (Spanish)
- Clinical Supervision Form
- Clinical Supervision Form(Spanish)
- Consent for Telehealth Services-English
- Consent for Telehealth Services-Spanish
- Consent for Treatment and Evaluation
- Consent for Treatment and Evaluation(Spanish)
- Consumer Handbook Acknowledgement
- Contexture ROI English
- Contexture ROI Spanish
- Intake / Annual Consent Packet
- Intake / Annual Consent Packet(Spanish)
- ROI - English
- ROI - Spanish
- [OLD] ROI English Advance Directive(18 +)
- SUD Release SUD Release(Spanish)
英文:
You can clean the items (to remove the prefix), sort it on the cleaned value and then return the original list of values ordered based on the cleaned value.
void Main()
{
List<string> items = new()
{
"[OLD] ROI English Advance Directive(18 +) ",
"Advance Directive(18 +) (Spanish)",
"Clinical Supervision Form ",
"Clinical Supervision Form(Spanish) ",
"Consent for Telehealth Services-English ",
"Consent for Telehealth Services-Spanish ",
"Consent for Treatment and Evaluation ",
"Consent for Treatment and Evaluation(Spanish) ",
"Consumer Handbook Acknowledgement ",
"Contexture ROI English ",
"Contexture ROI Spanish ",
"Intake / Annual Consent Packet ",
"Intake / Annual Consent Packet(Spanish) ",
"ROI - English ",
"ROI - Spanish ",
"SUD Release SUD Release(Spanish)",
};
var sortedItems = CustomSort(items, "[OLD] ");
}
public List<string> CustomSort(List<string> items, string prefix)
{
List<(string cleaned, string original)> cleaned = new();
foreach(var i in items)
cleaned.Add(new(i.Replace(prefix, string.Empty), i));
var sorted = cleaned.OrderBy(i => i.cleaned).ToList();
return sorted.Select(i => i.original).ToList();
}
sortedItems:
Advance Directive(18 +) (Spanish)
Clinical Supervision Form
Clinical Supervision Form(Spanish)
Consent for Telehealth Services-English
Consent for Telehealth Services-Spanish
Consent for Treatment and Evaluation
Consent for Treatment and Evaluation(Spanish)
Consumer Handbook Acknowledgement
Contexture ROI English
Contexture ROI Spanish
Intake / Annual Consent Packet
Intake / Annual Consent Packet(Spanish)
ROI - English
ROI - Spanish
[OLD] ROI English Advance Directive(18 +)
SUD Release SUD Release(Spanish)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论