c# - 如何用字母创建按字母顺序排列的列表?

标签 c# asp.net-mvc algorithm logic

大家好,我正在尝试按列实现 alpha 有序列表

picture所示

但是我的算法不清楚,也许有人可以帮助我

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);

在我的代码结果中,我看到丢失的字母 X Y Z Å ä Ö 它看起来像 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>
  <%} %>

请帮忙...

最佳答案

嗯,你说得对,代码不是特别清晰:)

我并没有真正遵循代码的主循环,但这里有一个更简单的起点。请注意,它不会正确地将 0-9 分组(目前它只处理 0):说实话,我不确定解决这个问题的最佳方法。您可能需要推迟该操作,直到收到一些与任何正常字母都不匹配的条目为止...

using System;
using System.Linq;

class Test
{   
    static void Main(string[] args)
    {
        ShowGroups();
    }

    private static readonly char[] Letters = 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ0".ToCharArray();

    // This is taking the place of EntityBase.db.Services
    // for the purposes of the test program
    public static string[] Services = { "Blogger", "Delicious", 
            "Digg", "Ebay", "Facebook", "Feed", "Flickr", 
            "Friendfeed", "Friendster", "Furl", "Google", 
            "Gosupermodel", "Lastfm", "Linkedin", "Livejournal",
            "Magnolia", "Mixx", "Myspace", "NetOnBuy", "Netvibes",
            "Newsvine", "Picasa", "Pownce", "Reddit", "Stumbleupon",
            "Technorati", "Twitter", "Vimeo", "Webshots", 
            "Wordpress" };

    public static void ShowGroups()
    {
        var groupedByLetter = 
            from letter in Letters
            join service in Services on letter equals service[0] into grouped
            select new { Letter = letter, Services = grouped };

        // Demo of how to access the groups
        foreach (var entry in groupedByLetter)
        {
            Console.WriteLine("=== {0} ===", entry.Letter);
            foreach (var service in entry.Services)
            {
                Console.WriteLine ("  {0}", service);
            }
            Console.WriteLine();
        }
    }
}

我不知道你打算如何将结果分成 5 个相等的列...

关于c# - 如何用字母创建按字母顺序排列的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/929416/

相关文章:

c# - 同一引用程序集的多个版本

c# - 线程如何节省时间?

algorithm - 词分布问题

algorithm - 如果图非常大,您将如何修改 BFS 以找到从 A 到 B 的最短路径?

c# - 如何访问 MVC .net 核心中返回的 Controller 的字段结果

c# - 如何以编程方式连接到 IIS 中托管的 WCF 服务

c# - Mvc 从服务器端取消 Jquery Ajax 请求

c# - & 符号被 HTML 编码为 &。有什么办法可以防止这种情况?

algorithm - 编写自己的平方根函数

c# - 如何将子 JSON 结构转换为 C# 对象图