我创建了这个for循环,以便单击按钮时可以查看类似以下内容的同学的成绩:
Name Test Basket Destructive Final Grade
Alex 10 10 10 10
Danny 20 20 20 20
Dave 35 40 50 40
Goerge 0 0 0 0
Hannan 0 0 0 0
Ian 0 0 0 0
Muna 0 0 0 0
这是它的代码:
private void uiShowMarksAZButton_Click(object sender, EventArgs e)
{
uiMarksDisplayTextBox.Text = string.Join("\t", Columns);
uiMarksDisplayTextBox.Text += System.Environment.NewLine;
for (int i = 0; i < Names.Length; i++)
{
uiMarksDisplayTextBox.Text += Names[i];
for (int x = 0; x < 3; x++)
{
uiMarksDisplayTextBox.Text += "\t" + Grade[i, x];
}
uiMarksDisplayTextBox.Text += "\t" + "\t" + FinalGrade[i];
uiMarksDisplayTextBox.Text += System.Environment.NewLine;
}
}
我希望能够对我的表进行排序,从最高的最终成绩开始,在顶部,最低的最低成绩在底部。这不仅应该对最终成绩进行排序,还应该对姓名和以前的成绩进行排序。
我希望不使用Array.Sort和Datagrid就能做到这一点。
在这里尝试:
private void uiShowMarks100_0Button_Click(object sender, EventArgs e)
{
uiMarksDisplayTextBox.Text = "";
uiMarksDisplayTextBox.Text = string.Join("\t", Columns);
uiMarksDisplayTextBox.Text += System.Environment.NewLine;
double temp = 0;
for (int x = 0; x < FinalGrade.Length; x++)
{
for (int j = x + 1; j < FinalGrade.Length; j++)
{
if (FinalGrade[x] > FinalGrade[j])
{
temp = FinalGrade[j];
FinalGrade[j] = FinalGrade[x];
FinalGrade[x] = temp;
}
uiMarksDisplayTextBox.Text += "\t" + "\t" + FinalGrade[x];
uiMarksDisplayTextBox.Text += System.Environment.NewLine;
}
}
}
一个例子是:
Name Test Basket Destructive Final Grade
Dave 35 40 50 40
Danny 20 20 20 20
Alex 10 10 10 10
Goerge 0 0 0 0
Hannan 0 0 0 0
Ian 0 0 0 0
Muna 0 0 0 0
答案 0 :(得分:2)
您可以使用它来对数据进行排序:
var input = new IComparable[][]
{
new IComparable[] {"Alex", 10, 10, 10, 10},
new IComparable[] {"Danny", 20, 20, 20, 20},
new IComparable[] {"Dave", 35, 40, 50, 40},
new IComparable[] {"Goerge",0 , 0 , 0 , 0 },
new IComparable[] {"Hannan",0 , 0 , 0 , 0 },
new IComparable[] {"Ian", 0 , 0 , 0 , 0 },
new IComparable[] {"Muna", 0 , 0 , 0 , 0 },
};
var indexOfColumnToOrderBy = 4; // "Final Grade" field
for (var i = 0; i < input.Length - 1; ++i)
{
for (var j = i + 1; j < input.Length; ++j)
{
IComparable[] t;
if (input[i][indexOfColumnToOrderBy].CompareTo(input[j][indexOfColumnToOrderBy]) < 0)
{
t = input[i];
input[i] = input[j];
input[j] = t;
}
}
}
我已将嵌套数组的每个项目都声明为IComparable
,因为string和int都实现了此接口,所以这就是为什么您可以使用方法CompareTo
这就是我们在21世纪的方法:创建一个Student
类:
private class Student
{
public string Name { get; set; }
public int Test { get; set; }
public int Basket { get; set; }
public int Destructive { get; set; }
public int FinalGrade { get; set; }
}
将输入定义为
var input = new Student[]
{
new Student {Name = "Alex", Test = 10, Basket = 10, Destructive = 10, FinalGrade = 10},
new Student {Name = "Danny", Test = 20, Basket = 20, Destructive = 20, FinalGrade = 20, },
new Student {Name = "Dave", Test = 35, Basket = 40, Destructive = 50, FinalGrade = 40, },
new Student {Name = "Goerge", Test = 0, Basket = 0, Destructive = 0, FinalGrade = 0, },
new Student {Name = "Hannan", Test = 0, Basket = 0, Destructive = 0, FinalGrade = 0, },
new Student {Name = "Ian", Test = 0, Basket = 0, Destructive = 0, FinalGrade = 0, },
new Student {Name = "Muna", Test = 0, Basket = 0, Destructive = 0, FinalGrade = 0, },
};
然后用Linq排序:
var sorted = input.OrderByDescending(student => student.FinalGrade).ToArray();
即使这种方法不能满足教授的要求,您也可以使用它来调试自己的算法
答案 1 :(得分:0)
对于原样的问题,这是一个高级算法:
首先确保所有数组的长度相同。
然后应用您喜欢的排序算法(我建议初学者使用冒泡排序。最差的排序算法,但最容易学习)。
每当交换FinalGrade数组中的元素时,都将交换相同索引的其他数组中的元素。
例如,如果在FinalGrade中交换了索引1和3,那么对于所有其他数组也将交换元素1和3。
我将实际编码保留为练习。
答案 2 :(得分:0)
我不认为您只能循环浏览一次。您可以尝试以下类似的递归逻辑来保持循环,直到所有内容都排序为止。
private List<YourObject> Sort(FinalGrade){
bool finished = true;
for (int x = 0; x < FinalGrade.Length; x++)
{
for (int j = x + 1; j < FinalGrade.Length; j++)
{
if (FinalGrade[x] > FinalGrade[j])
{
temp = FinalGrade[j];
FinalGrade[j] = FinalGrade[x];
FinalGrade[x] = temp;
finished = false;
}
}
}
return finished? FinalGrade : Sort(FinalGrade)
}