我将一个csv文件读入我的程序,现在我想用Icomparable和排序算法对“ RaceTime”进行排序。 RaceTime包含与比赛不同的时间,以“小时:分钟:秒”显示。如何使用Icomparable对struct元素进行排序?
起初,我尝试将时间转换为秒,并使用冒泡排序将其排序,这很好用,但是对于此程序,我必须使用Icomparable
struct Race : IComparable<Race>
{
public string StNr;
public string Name;
public string Birthyear;
public string Nationality;
public string RaceTime;
public int CompareTo(Race other)
{
return RaceTime.CompareTo(other.RaceTime);
}
}
static int[] SortTime(int[] Seconds)
{
int tmp = 0;
for (int i = 0; i < Seconds.Length - 1; i++)
{
for (int j = i + 1; j < Seconds.Length; j++)
{
if (Seconds[i] < Seconds[j])
{
tmp = Seconds[i];
Seconds[i] = Seconds[j];
Seconds[j] = tmp;
}
}
Console.WriteLine($"{Seconds[i]}");
}
return Seconds;
}
}