Guest User

Untitled

a guest
Nov 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Practice_1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. // List版
  15. var students = File.ReadLines("data.csv", Encoding.GetEncoding(932))
  16. .Skip(1)
  17. .Select(row =>
  18. {
  19. var cols = row.Split(',');
  20.  
  21. return new Student
  22. {
  23. StudentName = cols[0],
  24. EnglishPoint = int.Parse(cols[1]),
  25. MathPoint = int.Parse(cols[2]),
  26. SceincePoint = int.Parse(cols[3]),
  27. };
  28. });
  29.  
  30. // リスト版
  31. var studentsList = students.ToList();
  32. // 配列版
  33. var studentsArray = students.ToArray();
  34. // List.Sort
  35. new List<Student> { }.Sort((x, y) => x.EnglishPoint - y.EnglishPoint);
  36. // Array.Sort
  37. // ↑と↓とで使い方が違うみたい。
  38. Array.Sort(studentsList.ToArray(), (x, y) => x.EnglishPoint - y.EnglishPoint);
  39.  
  40. // Sortだとリストでも、配列でもなんでも使える。
  41. // (厳密にいうとIEnumerableインターフェースを実装していれば)
  42. var sortedStudentsFromList = studentsList.OrderBy(student => student.EnglishPoint);
  43. var sortedStudentsFromArray = studentsArray.OrderBy(student => student.EnglishPoint);
  44. }
  45.  
  46. public class Student
  47. {
  48. public string StudentName { get; set; }
  49.  
  50. public int EnglishPoint { get; set; }
  51.  
  52. public int MathPoint { get; set; }
  53.  
  54. public int SceincePoint { get; set; }
  55. }
  56. }
  57. }
Add Comment
Please, Sign In to add comment