为了C#按字母顺序列表 [英] Order C# list alphabetically

查看:122
本文介绍了为了C#按字母顺序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的下列code从网页API获取数据并以列表填充它。

I am using to following code to get data from a Web Api and populate it in a list.

HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("http://localhost:12345/api/items");

var info = new List<SampleDataGroup>();


            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

               var item = JsonConvert.DeserializeObject<dynamic>(content);


                foreach (var data in item)
                {
                      var infoSect = new info
                            (

                                (string)data.Id.ToString(),
                                (string)data.Name,
                                (string)"",
                                (string)data.PhotoUrl,
                                (string)data.Description

                            );
                                 info.Add(infoSect);
                }
             }
            else
            {
                MessageDialog dlg = new MessageDialog("Error");
                await dlg.ShowAsync();
            }


            this.DefaultViewModel["Sections"] = info;

如何通过名称字母顺序呢?因此,所显示的结果是从A-Z由它的名称进行排序。

How do I order this alphabetically by Name? So that the results shown are ordered from A-Z by its Name.

推荐答案

我建议你试试这个:

var sorted = info.OrderBy(i => i.Name);

这将返回排序的数据,在传递给排序依据法恩pression选择的字段排序。对于字符串数据的默认比较将是字母排序,这应该足以满足您的需求。

This will return sorted data, ordered by the field chosen in the expression passed to the OrderBy method. The default comparison for string data will be alphabetic sorting, which should be sufficient for your needs.

如果您需要分配给DefaultViewModel [节]将返回一个列表,你可以这样做:

If you require a List to be returned for assigning to DefaultViewModel["Sections"], you can do:

this.DefaultViewModel["Sections"] = info.OrderBy(i => i.Name).ToList();

这篇关于为了C#按字母顺序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆