Reputation: 9225
I have the following:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < contentList.Count;i++)
{
sb.Append("<div class=\"justPad\">");
sb.Append("<a class=\"defaultLinks\" ");
sb.Append("href=" + contentList[i].Quicklink + " ");
sb.Append("title=" + contentList[i].Title + ">" + contentList[i].Title + "</a>");
sb.Append("</div>");
}
Label2.Text = sb.ToString();
The result is:
<span id="ctl00_BodyPlaceHolder_Label2">
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3198" title="Hammond, John">Hammond, John</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3205" title="Beezer, Michelle">Beezer, Michelle</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3208" title="Tops, John">Tops, John</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3209" title="Kew, Lauren">Kew, Lauren</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=352" title="Perez, Lance">Perez, Lance</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=353" title="Powell, French">Powell, French</a></div>
</span>
As you can see the order of display is not sorted.
How can I sort the StringBuilder based on contentList[i].Title
so end result is:
<span id="ctl00_BodyPlaceHolder_Label2">
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3205" title="Beezer, Michelle">Beezer, Michelle</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3198" title="Hammond, John">Hammond, John</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3209" title="Kew, Lauren">Kew, Lauren</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=352" title="Perez, Lance">Perez, Lance</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=353" title="Powell, French">Powell, French</a></div>
<div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3208" title="Tops, John">Tops, John</a></div>
</span>
Upvotes: 0
Views: 973
Reputation: 236208
It's simple - sort content list before building list of links:
foreach (var content in contentList.OrderBy(c => c.Title))
{
sb.Append("<div class=\"justPad\">");
sb.Append("<a class=\"defaultLinks\" ");
sb.Append("href=\"" + content.Quicklink + "\" ");
sb.Append("title=\"" + content.Title + "\">" + content.Title + "</a>");
sb.Append("</div>");
}
Consider also using interpolated strings to make your code more readable:
sb.Append($"<a class='defaultLinks' href='{content.Quicklink}' title='{content.Title}'>");
Note: consider to use HtmlTextWriter if you are creating some html. You can write to string (as with StringBuilder) or directly to any other TextWriter to avoid creating big in-memory strings:
// using System.Web.UI
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, "ctl00_BodyPlaceHolder_Label2");
writer.RenderBeginTag(HtmlTextWriterTag.Span);
foreach (var content in contentList.OrderBy(c => c.Title))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "justPad");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Class, "defaultLinks");
writer.AddAttribute(HtmlTextWriterAttribute.Href, content.Quicklink);
writer.AddAttribute(HtmlTextWriterAttribute.Title, content.Title);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.WriteEncodedText(content.Title);
writer.RenderEndTag();
writer.RenderEndTag();
}
writer.RenderEndTag();
}
Upvotes: 1