Как отсортировать массив, созданный с помощью конструктора с двумя переменными? c#

Рейтинг: 0Ответов: 1Опубликовано: 28.02.2023
using System;
using System.Linq;
using System.Collections.Generic;

namespace практика1
{
    struct Sportsmen
    {
        private string famile;
        private double rez;

        public string Famile { get { return famile; } set { famile = value; } }
        public double Rez { get { return rez; } set { rez = value; } }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Sportsmen[] temp = new Sportsmen[5];
            string[] arr = new string[5] { "Иванов", "Sidorov", "Petrov", "Amongusov", "Susov" };
            double[] rez = new double[5] { 3.4, 6.2, 7.3, 9.1, 6.6 };

            for (int i = 0; i < temp.Length; i++)
            {
                temp[i].Famile = arr[i];
                temp[i].Rez = rez[i];
                Console.WriteLine("Фam {0} \t Res {1:f2}", temp[i].Famile, temp[i].Rez);
            }

            temp.OrderBy(p => p.Rez);

            for (int i = 0; i < temp.Length; i++)
            {
                Console.WriteLine("Фam {0} \t Res {1:f2}", temp[i].Famile, temp[i].Rez);
            }
        }
    }
}

После temp.OrderBy(p => p.Rez); ничего не меняется

Ответы

▲ 0

Нужно присвоить результат переменной. Так же не стоит забывать про ToArray, потому что OrderBy возвращает не Sportsmen[], а IOrderedEnumerable

Sportsmen[] sportsmens = temp.OrderBy(p => p.Rez).ToArray()

https://learn.microsoft.com/ru-ru/dotnet/api/system.linq.enumerable.orderby?view=net-7.0