解决方案 »

  1.   

    将重量和密度分别用两个SortedList存起来(SortedList是基于键排序的)  然后遍历SortedList的值和每一行的重量和密度比较,相等就取出键。  不知道我说明白了没
      

  2.   


    我是初学者,暂时还没有用过SortedList,所以说我现在没看明白。。能否简单写一段代码。。我再慢慢学习一下如何使用这个。。
      

  3.   

    打个比方, 你遍历一下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;
                }
            }
      

  4.   

    这个更简单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;
                }
            }