上のデータ例を見る限り、StudentDate1~3は一種類のクラスで表現できる?
であれば、
1)データを配列で受け取る
2)配列をnumberをキーに昇順にソート
3)ソートしたデータのnameを表示する
と処理するメソッドを作ればOKかと思います。
sample.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
public class StudentData
{
public string Name { get; init; }
public int Number { get; init; }
}
internal class Program
{
/// <summary>
/// numberの小さい順にnameを表示する
/// </summary>
/// <param name="students"></param>
public static void ShowStudentName( IEnumerable<StudentData> students )
{
foreach ( var student in students.OrderBy( x => x.Number ) ) {
Console.WriteLine( student.Name );
}
}
private static void Main()
{
var students = new[] {
new StudentData { Name = "aikawa", Number = 1 },
new StudentData { Name = "tanaka", Number = 15 },
new StudentData { Name = "sato", Number = 13 },
};
ShowStudentName( students );
}
}
}
2Like