Aca Licic
Aca Licic

Reputation: 1

Linq Collection Sorting

I need to sort countries in this collection using LINQ:

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

It should be sorted by population and printed that way.

Upvotes: 0

Views: 287

Answers (3)

ErikMuir
ErikMuir

Reputation: 181

You could convert the array into a List, order it by population, and then convert it back into an array.

countryCollection = countryCollection.ToList().OrderBy(x => x.Population).ToArray();

Or if you want to order it by descending population instead:

countryCollection = countryCollection.ToList().OrderByDescending(x => x.Population).ToArray();

Upvotes: 0

Bedir
Bedir

Reputation: 586

You can use a list or if you want to maintain an array you can use a delegate to anonymous method:

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

        }

See http://www.csharp-examples.net/sort-array/ for more details.

Upvotes: 0

João Paulo Amorim
João Paulo Amorim

Reputation: 436

This should do what you want

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

}

Upvotes: 1

Related Questions