如何按字母顺序对这个MVC下拉列表进行排序,但忽略括号?

huangapple go评论70阅读模式
英文:

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界面的截图:

如何按字母顺序对这个MVC下拉列表进行排序,但忽略括号?

英文:

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:

&lt;div class=&quot;form-inline&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label for=&quot;consentFormSelect&quot;&gt;Consent Form : &lt;/label&gt;
        &lt;select id=&quot;consentFormSelect&quot; class=&quot;form-control&quot; onchange=&quot;onConsentFormSelectChange()&quot;&gt;
            &lt;option value=&quot;&quot;&gt;Select An Option&lt;/option&gt;
            @foreach (var item in Model.ConsentForms.OrderBy(m =&gt; m.CodeDesc.ToString()).ToList())
            {
                &lt;option value=&quot;@item.CodeId&quot;&gt;@item.CodeDesc&lt;/option&gt;
            }
        &lt;/select&gt;
    &lt;/div&gt;
&lt;/div&gt;

Screenshot of the UI:

如何按字母顺序对这个MVC下拉列表进行排序,但忽略括号?

答案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(&quot;[&quot;, &quot;&quot;).Replace(&quot;]&quot;, &quot;&quot;); }
  }
...
}

Then:

@foreach (var item in Model.ConsentForms.OrderBy(m =&gt; 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&lt;string&gt; items = new()
	{
		&quot;[OLD] ROI English Advance Directive(18 +) &quot;,
		&quot;Advance Directive(18 +) (Spanish)&quot;,
		&quot;Clinical Supervision Form &quot;,
		&quot;Clinical Supervision Form(Spanish) &quot;,
		&quot;Consent for Telehealth Services-English &quot;,
		&quot;Consent for Telehealth Services-Spanish &quot;,
		&quot;Consent for Treatment and Evaluation &quot;,
		&quot;Consent for Treatment and Evaluation(Spanish) &quot;,
		&quot;Consumer Handbook Acknowledgement &quot;,
		&quot;Contexture ROI English &quot;,
		&quot;Contexture ROI Spanish &quot;,
		&quot;Intake / Annual Consent Packet &quot;,
		&quot;Intake / Annual Consent Packet(Spanish) &quot;,
		&quot;ROI - English &quot;,
		&quot;ROI - Spanish &quot;,
		&quot;SUD Release SUD Release(Spanish)&quot;,
	};
	
	var sortedItems = CustomSort(items, &quot;[OLD] &quot;);
}

public List&lt;string&gt; CustomSort(List&lt;string&gt; items, string prefix)
{
	List&lt;(string cleaned, string original)&gt; cleaned = new();
	
	foreach(var i in items)
		cleaned.Add(new(i.Replace(prefix, string.Empty), i));
	var sorted = cleaned.OrderBy(i =&gt; i.cleaned).ToList();
	return sorted.Select(i =&gt; 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)

huangapple
  • 本文由 发表于 2023年6月22日 03:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定