本文介绍了二维数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要对二维数组进行排序。每天我处理一个文件,其中包含9个/ b $ b逗号分隔的字段和不同数量的记录(行)。我想 拉入每一行,将它按照逗号分成二维 数组(Perl让它变成crissake),并按一个排序的字段(购买 订单#)。麻烦的是,Array.Sort只支持单维数组。 最初我想制作一个锯齿状数组,取出购买 Order元素,将其分配给一个键值对应单个数组 行,排序并重新构造行,排序。很容易用英语说,不是很好的b / b 很容易为C#noob。 - Steve Wasser http://xdissent.com 逆向社会话语和神经质的意见 解决方案 这里有一个如何对锯齿状数组进行排序的例子。要对任何数组进行排序,你需要告诉框架如何对 元素进行逐例比较。这是通过提供一个实现IComparer的类型来完成的。 这里一个名为ArrayComparer的IComparer比较一维数组,比较某个特定索引处的元素。 。 /> 公共类ArrayComparer:System.Collections.IComparer { int ix; public ArrayComparer( int SortFieldIndex) { ix = SortFieldIndex; } public int Compare(对象) x,对象y) { IComparable cx =(IComparable)((Array)x)。GetValue(ix); IComparable cy =(IComparable)((Array)y)。GetValue(ix); 返回cx.CompareTo(cy); } } ///< summary> ///应用程序的主要入口点。 ///< / summary> [STAThread] static void Main(string [] args) { string [] [] lines = new string [4] []; lines [0] = new string [] {" 1"," a" ;," d"}; lines [1] = new string [] {" 2"," b"," c"}; lines [2] = new string [] {" 3"," c"," b"}; lines [3] = new string [] {" 4"," ; d"," a"}; foreach(字符串[]行中的行) { 控制台。 WriteLine(第[0行]行); } System.Array.Sort(行,新的ArrayComparer(2)); foreach( string [] line in lines) { Console.WriteLine(line [0]); } } David I need to sort a two-dimensional array. Each day I process a file with 9comma-delimited fields, and varying amount of records (lines). I want topull in each line, split it according to the comma into a two dimensionalarray (Perl has it f''r crissake), and sort by one of the fields (PurchaseOrder #). Trouble is, Array.Sort only supports single dimension arrays.Originally I was thinking of making a jagged array, pulling out the PurchaseOrder element, assigning it to a key value corresponding to the single arrayline, sort and re-construct the lines, sorted. Easy to say in English, notso easy for a C# noob.--Steve Wasser http://xdissent.comthe journal of contrarian social discourse and neurotic opinion 解决方案Here''s an example of how to sort a jagged array. To sort any array, youjust need to tell the framework how to perform pariwise comparisons on theelements. This is done by supplying a type implementing IComparer.Here an IComparer called ArrayComparer compares 1-dimensional arrays bycomparing elements at some particular index.public class ArrayComparer : System.Collections.IComparer{int ix;public ArrayComparer(int SortFieldIndex){ix = SortFieldIndex;}public int Compare(object x, object y){IComparable cx = (IComparable)((Array)x).GetValue(ix);IComparable cy = (IComparable)((Array)y).GetValue(ix);return cx.CompareTo(cy);}}/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main(string[] args){string[][] lines = new string[4][];lines[0] = new string[] {"1","a","d"};lines[1] = new string[] {"2","b","c"};lines[2] = new string[] {"3","c","b"};lines[3] = new string[] {"4","d","a"};foreach (string[] line in lines){Console.WriteLine(line[0]);}System.Array.Sort(lines,new ArrayComparer(2));foreach (string[] line in lines){Console.WriteLine(line[0]);}}David 这篇关于二维数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 17:21