本文采用冒泡排序法对二维数组进行排序代码内容如下
using System; namespace experment4 { class Program { private static int result; private static int result1; static void Main(string[] args) { string[,] a = new String[5,2]; a[0, 0] = "2"; a[0, 1] = "Smith"; a[1, 0] = "4"; a[1, 1] = "John"; a[2, 0] = "5"; a[2, 1] = "Mary"; a[3, 0] = "1"; a[3, 1] = "Cherr"; a[4, 0] = "3"; a[4, 1] = "Tom"; Console.WriteLine("排序前:"); Console.Write("学号:"); for(int i = 0; i < 5; i++) { Console.Write(a[i, 0].ToString().PadRight(8,' ')); } Console.WriteLine(); Console.Write("姓名:"); for(int j = 0; j < 5; j++) { Console.Write(a[j, 1].ToString().PadRight(8, ' ')); } Console.WriteLine(); //按学号排序 Console.WriteLine("按学号排序后:"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { if(j!=4-i) result = string.Compare(a[j , 0], a[j+1,0],true); if (j!=4-i&&result >0) { string temp = a[j, 0]; a[j, 0] = a[j + 1, 0]; a[j + 1, 0] = temp; string temp2 = a[j, 1]; a[j, 1] = a[j + 1, 1]; a[j + 1, 1] = temp2; } } } Console.Write("学号:"); for (int i = 0; i < 5; i++) { Console.Write(a[i, 0].ToString().PadRight(8, ' ')); } Console.WriteLine(); Console.Write("姓名:"); for (int i = 0; i < 5; i++) { Console.Write(a[i, 1].ToString().PadRight(8, ' ')); } Console.WriteLine(); //按姓名排序 Console.WriteLine("按姓名排序后:"); Console.WriteLine(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5 - i; j++) { if (j != 4 - i) result1 = string.CompareOrdinal(a[j, 1], a[j + 1, 1]); if (j != 4 - i && result1 >= 1) { string temp = a[j, 1]; a[j, 1] = a[j + 1, 1]; a[j + 1, 1] = temp; string temp2 = a[j, 0]; a[j, 0] = a[j + 1, 0]; a[j + 1, 0] = temp2; } } } Console.Write("学号:"); for (int i = 0; i < 5; i++) { Console.Write(a[i, 0].ToString().PadRight(8, ' ')); } Console.WriteLine(); Console.Write("姓名:"); for (int i = 0; i < 5; i++) { Console.Write(a[i, 1].ToString().PadRight(8, ' ')); } Console.WriteLine(); } } }
运行结果:
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算