日期:2014-05-18  浏览次数:20853 次

数组排序出了问题??
6-3-70
6-2-89
5-3-87.14
5-2-111.59
4-2-76.8
4-1-93.13
3-12-122.65
3-13-136.91
2-16-127.28
2-22-149.5
1-14-102.06
1-6-105.87
有这样一个二维数组,循环读出后如上。希望按照第三列,就是[0,2]来降序排列。请看我写面的排序方法,怎么毫无变化呢?

public static string[,] BubbleSort(string[,] list)
{
  for (int i = 0; i < list[0, 0].Length; i++)
  {
  for (int j = i; j < list[0, 0].Length; j++)
  {
  if (float.Parse(list[i,2]) < float.Parse(list[j,2]))
  {
  float temp = float.Parse(list[i, 2]);
  list[i,2] = Convert.ToString(list[j,2]);
  list[j,2] = Convert.ToString(temp);
  }
  }
  }  
  return list;
}

array4 = BubbleSort(array4); //调用方法;

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
public static float[,] BubbleSort(float[,] list)
{

for (int i = 0; i < list.GetLength(0); i++)
{
for (int j = i; j < list.GetLength(0); j++)
{
if (list[i, 2] < list[j, 2])
{
float temp = list[i, 2];
list[i, 2] = list[j, 2];
list[j, 2] = temp;
}
}


return list;

static void Main(string[] args)
{
float[,] flo = new float[,] { {6,3,70},
{ 6,2,89},
{5,3,87.14f},
{5,2,111.59f},
{4,2,76.8f},
{ 4,1,93.13f},
{3,12,122.65f},
{3,13,136.91f},
{2,16,127.28f},
{2,22,149.5f},
{ 1,14,102.06f},
{1,6,105.87f}
};
BubbleSort(flo);
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(flo[i, j].ToString()+"-");
}
Console.WriteLine();
}

Console.ReadLine();
}
}
}