C#是否可对表格数据进行按数值大小排序,代码怎么编辑?小弟我是初学兼自学者。求帮忙
C#是否可对表格数据进行按数值大小排序,代码如何编辑??我是初学兼自学者。。求帮忙


如何由表1得到表2??
------解决思路----------------------
打个比方, 你遍历一下table里的数据行,就会得到每行中的个体、重量、密度值,然后加入到SortedList里面, 最后循环SortedList得到对应的下标
------解决思路----------------------
这个更简单
如何由表1得到表2??
------解决思路----------------------
打个比方, 你遍历一下table里的数据行,就会得到每行中的个体、重量、密度值,然后加入到SortedList里面, 最后循环SortedList得到对应的下标
public void SortWeightAndDensity()
{
SortedList<double, int> weightSort = new SortedList<double, int>();
SortedList<double, int> densitySort = new SortedList<double, int>();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
weightSort.Add(Convert.ToDouble(row.Cells["重量"]), Convert.ToInt32(row.Cells["个体"]));
densitySort.Add(Convert.ToDouble(row.Cells["密度"]), Convert.ToInt32(row.Cells["个体"]));
}
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
double weight = Convert.ToDouble(row.Cells["重量"]);
double density = Convert.ToDouble(row.Cells["密度"]);
int weightIndex, densityIndex;
weightSort.TryGetValue(weight, out weightIndex);
row.Cells["按重量排序"].Value = weightIndex + 1;
weightSort.TryGetValue(density, out densityIndex);
row.Cells["按密度排序"].Value = densityIndex + 1;
}
}
------解决思路----------------------
这个更简单
public void SortWeightAndDensity()
{
List<double> weightSort = new List<double>();
List<double> densitySort = new List<double>();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
weightSort.Add(Convert.ToDouble(row.Cells["重量"]));
densitySort.Add(Convert.ToDouble(row.Cells["密度"]));
}
weightSort.Sort();
weightSort.Reverse();
densitySort.Sort();
densitySort.Reverse();
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
double weight = Convert.ToDouble(row.Cells["重量"]);
double density = Convert.ToDouble(row.Cells["密度"]);
row.Cells["按重量排序"].Value = weightSort.IndexOf(weight) + 1;
row.Cells["按密度排序"].Value = densitySort.IndexOf(density) + 1;
}
}