关于实体类的排序输出问题
延续之前的问题,实体类的排序输出问题。
/// <summary>
/// 实现IComparable接口,用kcNum做比较
/// </summary>
/// <param name="obj">比较对象</param>
/// <returns>比较结果</returns>
public class KeChen : IComparable
{
public string kcName { get; set; }
public int kcNum { get; set; }
public int CompareTo(object obj)
{
if (obj is KeChen)
{
return kcNum.CompareTo(((KeChen)obj).kcNum);
}
return 1;
}
}
//查找子串的个数
private int findstring(string str1, string str2)
{
int i, j;
int str1len = str1.Length, str2len = str2.Length;
int count = 0;
for (i = 0; i < str1len - str2len + 1; i++)
{
for (j = 0; j < str2len; j++)
{
if (str2[j] != str1[i + j]) break;
}
if (j == str2len) count++;
}
return count;
}
private void button1_Click(object sender, EventArgs e)
{
//……
//最满意的任课老师 数据准备
strSQL = "select * from dc_teacher";
cmd = new SqlCommand(strSQL, conn);
sda.SelectCommand = cmd;
sda.Fill(ds, "BB");
strSQL = "select ID,P4 from dc_vote where SYSID='" + myData.SYS_ID + "'";
cmd = new SqlCommand(strSQL, conn);
sda.SelectCommand = cmd;
sda.Fill(ds, "DD");
int[] strTnum1 = new int[ds.Tables["BB"].Rows.Count];
for (int i = 0; i < ds.Tables["BB"].Rows.Count; i++)
{
string strID = ds.Tables["BB"].Rows[i][0].ToString();
strTnum1[i] = 0;
for (int j = 0; j < ds.Tables["DD"].Rows.Count; j++)
{
string strP4 = ds.Tables["DD"].Rows[j][1].ToString();
strTnum1[i] = strTnum1[i] + findstring(strP4, strID);
}
}
string strTeacher = "";
//初始化教师并排序 最满意的任课教师
KeChen[] T1 = new KeChen[ds.Tables["BB"].Rows.Count];
for (int i = 0; i < ds.Tables["BB"].Rows.Count; i++)
{
T1[i] = new KeChen() { kcName = ds.Tables["BB"].Rows[i][1].ToString(), kcNum = strTnum1[i] };
}
Array.Sort(T1);
Array.ForEach<KeChen>(T1, (s) =>
{
strTeacher = strTeacher + s.kcName + " " + s.kcNum.ToString() + " ";
});
//下面输出教师名称+满意度strTeacher
//……
}