Alphabetic ordering c #

1

I have a program in C #, that entering the names of three countries with their respective temperatures, calculates the average annual temperature of each one. Then he prints them in alphabetical order. When finished, print the country with the highest average quarterly temperature.

My precise query is the following:

How do I order alphabetically in C #?

Example:

Entry

  

Bolivia, Argentina, Albania.

Exit

  

Albania, Argentina, Bolivia.

I tried to develop it in this way, but there was no case:

public void ImprimirTempTrimestrales()
{
    Console.WriteLine("Las temperaturas trimestrales: ");
    for (int c = 0; c < 3; c++)
    {
        for (int f = 0; f < 3 - 1 - c; f++)
        {
            if (paises[f].CompareTo(paises[f + 1]) > 0)
            {
                string auxpais;
                auxpais = paises[f];
                paises[f] = paises[f + 1];
                paises[f + 1] = auxpais;

            }

            Console.WriteLine(paises[f] + " " + temptri[f]);
        }
    }
}

All the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Matriz
 {
  class Matriz
   {
    private string[] paises;
    private int[,] tempmen;
    private int[] temptri;

    public void Cargar()
    {
        paises = new String[3];
        tempmen = new int[4, 3];
        for (int f = 0; f < paises.Length; f++)
        {
            Console.Write("Ingrese nombre del país:");
            paises[f] = Console.ReadLine();
            for (int c = 0; c < tempmen.GetLength(1); c++)
            {
                Console.Write("Ingrese temperatura mensual:");
                string linea = Console.ReadLine();
                tempmen[f, c] = int.Parse(linea);
            }
        }
    }

    public void ImprimirTempMensuales()
    {
        for (int f = 0; f < paises.Length; f++)
        {
            Console.Write("Pais:" + paises[f] + ":");
            for (int c = 0; c < tempmen.GetLength(1); c++)
            {
                Console.Write(tempmen[f, c] + " ");
            }
            Console.WriteLine();
        }
    }

    public void CalcularTemperaturaAnual()
    {
        temptri = new int[12];
        for (int f = 0; f < tempmen.GetLength(0); f++)
        {
            int suma = 0;
            for (int c = 0; c < tempmen.GetLength(1); c++)
            {
                suma = suma + tempmen[f, c];
            }
            temptri[f] = suma / 12;
        }
    }

    public void ImprimirTempTrimestrales()
    {
        Console.WriteLine("Las temperaturas trimestrales: ");
        for (int c = 0; c < 3; c++)
        {
            for (int f = 0; f < 3 - 1 - c; f++)
            {
                if (paises[f].CompareTo(paises[f + 1]) > 0)
                {
                    string auxpais;
                    auxpais = paises[f];
                    paises[f] = paises[f + 1];
                    paises[f + 1] = auxpais;

                }

                Console.WriteLine(paises[f] + " " + temptri[f]);
            }
        }
    }
    public void PaisMayorTemperaturaTri()
    {
        int may = temptri[0];
        string nom = paises[0];
        for (int f = 0; f < paises.Length; f++)
        {
            if (temptri[f] > may)
            {
                may = temptri[f];
                nom = paises[f];
            }
        }
        Console.WriteLine("El pais que tiene la temperatura trimestral mayor es: " + nom + " y es de: " + may);
    }

    static void Main(string[] args)
    {
        Matriz10 ma = new Matriz10();
        ma.Cargar();
        ma.ImprimirTempMensuales();
        ma.CalcularTemperaturaAnual();
        ma.ImprimirTempTrimestrales();
        ma.PaisMayorTemperaturaTri();
        Console.ReadKey();
    }
  }
}

I appreciate any help.

    
asked by Mario 25.10.2018 в 06:05
source

1 answer

6

If you already have an array with the list of countries, it would be enough to call the method of LINQ OrderBy() :

var paisesOrdenados = paises.OrderBy(x => x);

It should be noted that paisesOrdenados will be a IOrderedEnumerable<string> if you want to store it also in an array you can use at the end .ToArray()

var paisesOrdenados = paises.OrderBy(x => x).ToArray();

If you want to modify the same variable, just replace the previous one.

paises = paises.OrderBy(x => x).ToArray();

Another way would be to pass the values of the array to List<string> and then call the .Sort() method.

var paisesList = paises.ToList();
paisesList.Sort();
    
answered by 25.10.2018 / 06:47
source