msp0310
11/16/2017 - 3:37 PM

advice_sort.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice_1
{
    class Program
    {
        static void Main(string[] args)
        {
            // List版
            var students = File.ReadLines("data.csv", Encoding.GetEncoding(932))
                .Skip(1)
                .Select(row =>
                {
                    var cols = row.Split(',');

                    return new Student
                    {
                        StudentName = cols[0],
                        EnglishPoint = int.Parse(cols[1]),
                        MathPoint = int.Parse(cols[2]),
                        SceincePoint = int.Parse(cols[3]),
                    };
                });

            // リスト版
            var studentsList = students.ToList();
            // 配列版
            var studentsArray = students.ToArray();
            // List.Sort
            new List<Student> { }.Sort((x, y) => x.EnglishPoint - y.EnglishPoint);
            // Array.Sort
            // ↑と↓とで使い方が違うみたい。
            Array.Sort(studentsList.ToArray(), (x, y) => x.EnglishPoint - y.EnglishPoint);

            // Sortだとリストでも、配列でもなんでも使える。
            // (厳密にいうとIEnumerableインターフェースを実装していれば)
            var sortedStudentsFromList = studentsList.OrderBy(student => student.EnglishPoint);
            var sortedStudentsFromArray = studentsArray.OrderBy(student => student.EnglishPoint);
        }

        public class Student
        {
            public string StudentName { get; set; }

            public int EnglishPoint { get; set; }

            public int MathPoint { get; set; }

            public int SceincePoint { get; set; }
        }
    }
}