Hi all I trying to implement alpha ordered list by columns
as shown on picture
But my algorithm is not clear and maybe someone could help me within
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);
In a result of my code I see missed letters X Y Z Å Ä Ö and it's looks like this
Here comes code that renders dictionary
<% 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>
<%} %>
Please help...