이 목록의 이름을 나이별로 정렬하고 싶습니다. (나는 지금까지 해본 적이 있습니다.)하지만이 이름들을 어떻게 다시 정렬 할 수 있을지 궁금합니다. 새 파일로 인쇄하기 전에 성을 입력하십시오. 예를 들어 다른 성으로 20 살인 5 명이있는 경우 어떻게 그 5 명을 알파벳순으로 오름차순으로 만들 수 있습니까?성과 이름으로 이름 목록 정렬 C#
class Person : IComparable
{
string vorname;
string nachname;
int age;
public Person(string vorname, string nachname, int age)
{
this.age = age;
this.nachname = nachname;
this.vorname = vorname;
}
public int CompareTo(object obj)
{
Person other = (Person)obj;
int a = this.age - other.age;
if (a != 0)
{
return -a;
}
else
{
return age.CompareTo(other.age);
}
}
public override string ToString()
{
return vorname + " " + nachname + "\t" + age;
}
}
class Program
{
static void Main(string[] args)
{
Person[] peeps = new Person[20];
try
{
StreamReader sr = new StreamReader("inputNames.txt");
int count = 0;
while (!sr.EndOfStream)
{
string data = sr.ReadLine();
Console.WriteLine();
string[] info = data.Split(',');
peeps[count] = new Person(info[0], info[1], int.Parse(info[2]));
count++;
}
Array.Sort(peeps);
sr.Close();
}
catch(FileNotFoundException e)
{
Console.WriteLine(e.Message);
}
StreamWriter sw = new StreamWriter("outputNames.txt");
Console.WriteLine();
foreach (Person p in peeps)
{
Console.WriteLine(p);
sw.WriteLine(p);
}
sw.Close();
}
}
linq 없이는 IComparable을 사용해야합니다. –