Как отсортировать массив по возрастанию
Нашел подобный код и он выдает вот такую ошибку

Весь мой код
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinAPP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Data
{
public static int Value1 { get; set; }
public static int Value2 { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
{
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
int cols = Data.Value1;
int row = Data.Value2;
dataGridView1.ColumnCount = cols;
dataGridView1.RowCount = row;
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView2.Rows.Clear();
int cols = Data.Value1;
int row = Data.Value2;
int k;
int p = 0;
float[,] array = new float[row, cols];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
array[i, j] = Convert.ToSingle(dataGridView1.Rows[i].Cells[j].Value);
}
}
for (int j = 0; j <cols; j++)
{
k = 1;
for (int i = 0; i < row; i++)
{
if (array[i, j] <= 0)
{
k = 0; break;
}
}
if (k != 0) p++;
}
label1.Text = "P = " + p.ToString();
dataGridView2.ColumnCount = dataGridView1.ColumnCount;
dataGridView2.RowCount = dataGridView1.RowCount;
if (p <= 0)
{
for (k = 0; k < row * cols; ++k)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (j != cols - 1)
{
if (array[i, j + 1] < array[i, j])
{
float tmp = array[i, j + 1];
array[i, j + 1] = array[i, j];
array[i, j] = tmp;
}
}
else
{
if ((array[i+1, 0] < array[i, j]) && (i != row - 1))
{
float tmp = array[i + 1, 0];
array[i + 1, 0] = array[i,j];
array[i, j] = tmp;
}
}
}
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
dataGridView2.Rows[i].Cells[j].Value = array[i, j];
}
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 fr2 = new Form2();
fr2.Show();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button3_Click_1(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Закрыть программу?", "Подтвердите действие", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dialogResult == DialogResult.Yes)
{
Close();
}
else if (dialogResult == DialogResult.No)
{
dialogResult = DialogResult.None;
}
}
}
}
Есть код для сортировки по строкам, а мне нужно выполнить сортировку по столбцам
if (p <= 0)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int f = m - 1; f > j; f--)
if (array[i, f] > array[i, f - 1])
{
float tmp = array[i, f];
array[i, f] = array[i, f - 1];
array[i, f - 1] = tmp;
}
}
}
}
Внешний вид программы
Что у меня сортируется столбцы или строки?
Нужно: Столбцы матрицы упорядочить по возрастанию значений их элементов.

