如何用字母创建按字母顺序排列的列表?
大家好,我尝试按列实现 alpha 有序列表,
如 图片
但我的算法不清楚,也许有人可以帮助我在
string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z", "Å", "Ä", "Ö", "0-9" };
int j = 0, s = 0, i = 1;
var fullServices = (from se in EntityBase.db.Services
orderby se.Name
select se).ToList();
int total = fullServices.Count;
var grouped = (from l in letters
select new ServiceInfo
{
Letter = l,
Services = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).ToList(),
Total = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).Count()
}).ToList();
Dictionary<int, List<ServiceInfo>> result = new Dictionary<int, List<ServiceInfo>>();
changecell:
List<ServiceInfo> item = new List<ServiceInfo>();
while (j < letters.Count())
{
letterchange:
List<Service> _services = new List<Service>();
while (s < total)
{
if ((s == (5 + (total % 5 > i ? 1 : 0)) * i))
{
item.Add(new ServiceInfo() { Letter = letters[j], Services = _services });
result.Add(i, item);
if (i == 6)
goto exit;
i++;
goto changecell;
}
//start render services
if (fullServices.ElementAt(s).Name.StartsWith(letters[j]))
{
_services.Add(fullServices.ElementAt(s));
s++;//increment service in list
}
else //letter switch
{
item.Add(new ServiceInfo() { Letter = letters[j], Services = _services });
j++;
goto letterchange;
}
}//end render services
}
exit:
return View(result);
我的代码结果中,我看到丢失的字母 XYZ Å ä Ö 它看起来像 this
这是渲染字典的代码
<% foreach (KeyValuePair<int, List<BL.Models.Infos.ServiceInfo>> col in Model)
{ %>
<ul class="col">
<% foreach (var item in col.Value)
{ %>
<% if (!item.Services.Any())
{%>
<li class="disabled">
<h1>
<%= item.Letter %></h1>
</li>
<%}
else
{ %>
<li>
<h1>
<a href="/service/info/<%= item.Letter %>"><%= item.Letter %></a>
</h1>
</li>
<% foreach (var service in item.Services)
{ %>
<li><a href="/service/info/<%= service.Name %>"><%= service.Name %></a></li>
<%}
}
}%>
</ul>
<%} %>
请帮忙。 ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,你肯定是对的,代码不是特别清晰:)
我并没有真正遵循你的代码的主循环,但这里有一个更简单的起点。 请注意,它不会正确地将 0-9 分组(目前它只处理 0):说实话,我不确定解决这个问题的最佳方法。 你可能想推迟它,直到你得到一些与任何正常字母都不匹配的条目...
但我不知道你打算如何将结果分成 5 个相等的列...
Well, you're certainly right that the code's not particularly clear :)
I don't really follow the main loop of your code, but here's a simpler starting point. Note that it won't group 0-9 properly (it only deals with 0 at the moment): I'm not sure the best way to approach that, to be honest. You might want to put that off until you get some entries which don't match any of the normal letters...
I don't know how you intend to split the results into 5 equal columns though...
这可能还很遥远 - 我刚刚在记事本中完成了此操作(现在无法访问 IDE)。 看起来您正在尝试填充
Dictionary> result
实例,我假设您的View
方法了解如何在列中布局结果。这里是:
如果它是正确的,它可能不会是最快的实现,但它更具可读性。
This might be way off - I've just done this in Notepad (don't have access to an IDE right now). It looks like you're trying to populate the
Dictionary<int, List<ServiceInfo>> result
instance and I'm assuming that yourView
method understands how to layout the results in columns.Here goes:
If it's correct, it probably won't be the fastest implementation, but it's more readable.