首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linq集合排序

Linq集合排序
EN

Stack Overflow用户
提问于 2019-02-21 03:27:24
回答 3查看 167关注 0票数 0

我需要使用LINQ对此集合中的国家/地区进行排序:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Program
    {
        class Country
        {
            public string Name { get; set; }
            public int Population { get; set; }

            public Country(string name, int population)
            {
                Name = name;
                Population = population;
            }
        }

        static void Main(string[] args)
        {
            Country[] countryCollection = 
            { 
                new Country("Afghanistan", 34656032),
                new Country("Austria", 8857960),
                new Country("Brazil", 210147125),
                new Country("Denmark", 5789957),
                new Country("Russia", 144526636),
                new Country("China", 1403500365),
                new Country("Turkey", 80810525),
                new Country("Serbia", 7001444),
                new Country("Iraq", 37202572),
                new Country("San Marino", 33344) 
            };
        }
    }
}

它应该按人口排序,并以这种方式打印。

EN

回答 3

Stack Overflow用户

发布于 2019-02-21 03:38:45

这应该可以做你想要的事情

代码语言:javascript
复制
class Program
{
    static void Main(string[] args)
    {
        List<Country> countryCollection = new List<Country>() {
                                        new Country("Afghanistan",34656032),
                                        new Country("Austria", 8857960),
                                        new Country("Brazil", 210147125),
                                        new Country("Denmark", 5789957),
                                        new Country("Russia", 144526636),
                                        new Country("China", 1403500365),
                                        new Country("Turkey", 80810525),
                                        new Country("Serbia", 7001444),
                                        new Country("Iraq", 37202572),
                                        new Country("San Marino", 33344) };

        var OrderedCountries = countryCollection.OrderByDescending(x => x.Population).ToList();

        foreach (var country in OrderedCountries)
        {
            Console.WriteLine($"The country {country.Name} has {country.Population} people");
        }

    }
}

public class Country
{
    public string Name { get; set; }
    public int Population { get; set; }


    public Country(string name, int population)
    {
        Name = name;
        Population = population;
    }

}
票数 1
EN

Stack Overflow用户

发布于 2019-02-21 04:01:17

你可以使用一个列表,或者如果你想维护一个数组,你可以使用一个委托给匿名方法:

代码语言:javascript
复制
static void Main(string[] args)
        {

            Country[] countryCollection = {
                new Country("Afghanistan", 34656032),
                new Country("Austria", 8857960),
                new Country("Brazil", 210147125),
                new Country("Denmark", 5789957),
                new Country("Russia", 144526636),
                new Country("China", 1403500365),
                new Country("Turkey", 80810525),
                new Country("Serbia", 7001444),
                new Country("Iraq", 37202572),
                new Country("San Marino", 33344)
            };

            Array.Sort(countryCollection, delegate(Country country1, Country country2) {
                return country1.Population.CompareTo(country2.Population);
            });

            foreach (Country country in countryCollection) Console.WriteLine("Country name: " + country.Name + " Population: " + country.Population);
            Console.ReadKey();        

        }

有关更多详细信息,请参阅http://www.csharp-examples.net/sort-array/

票数 0
EN

Stack Overflow用户

发布于 2019-02-21 10:40:02

您可以将数组转换为列表,按填充进行排序,然后将其转换回数组。

代码语言:javascript
复制
countryCollection = countryCollection.ToList().OrderBy(x => x.Population).ToArray();

或者,如果您想要按人口降序进行排序:

代码语言:javascript
复制
countryCollection = countryCollection.ToList().OrderByDescending(x => x.Population).ToArray();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54793891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档