Sort by (enumeration)
1 Public enum SortBy 2 {3 Asc,4 Desc5 }
View Code
Array Bubble Sort method
1 Public classsortentity2 {3 Public Static int[] Sortarray (int[] array,sortby sortby)4 {5 intFlag;6 Switch(SortBy)7 {8 CaseSORTBY.ASC:9 for(inti =0; I < array. Length-1; i++)Ten { One for(intj =0; J < Array. Length-1I J + +) A { - if(Array[j] < Array[j +1]) - { theFlag =Array[j]; -ARRAY[J] = array[j +1]; -Array[j +1] =Flag; - } + } - } + Break; A CaseSortby.desc: at for(inti =0; I < array. Length-1; i++) - { - for(intj =0; J < Array. Length-1I J + +) - { - if(Array[j] > array[j +1]) - { inFlag =Array[j]; -ARRAY[J] = array[j +1]; toArray[j +1] =Flag; + } - } the } * Break; $ default:Panax Notoginseng Break; - } the + returnArray; A } the}
View Code
converting an int array of type string into an int array
1 Public classArrayhelper2 {3 Public Static int[] getintarrayfromstring (stringstringstr)4 {5 string[] array = Stringstr.split (',');6 intFlag;int[] Arrayintnum =New int[Array. Length];7 for(inti =0; I < array. Length-1; i++)8 {9 if(int. TryParse (Array[i], outFlag))Ten { OneArrayintnum[i] =Flag; A } - Else - { the Throw NewException ("The content contains non-int data"); - } - } - returnArrayintnum; + } -}
View Code
File Read array
1 Public classfileentity2 {3 Public stringAddress {Get;Set; }4 Private stringreadcontent ()5 {6 stringContent =string. Empty;7 if(File.exists ( This. Address))8 {9Content = File.readalltext ( This. address);Ten One } A returnContent; - } - Public int[] GetArray () the { - returnarrayhelper.getintarrayfromstring (ReadContent ()); - } -}
View Code
Test method
1 Public voidTest ()2 {3 int[] Intarray =New int[] {1,2,3,4,5 };4 int[] Txtarray =NewFileentity () {address =@"C:\TEST. TXT" }. GetArray ();5 int[] DataArray =Newdatabaseentity (). GetArray ();6Intarray =Sortentity.sortarray (Intarray, SORTBY.ASC);7Txtarray =Sortentity.sortarray (Txtarray, SORTBY.ASC);8DataArray =Sortentity.sortarray (DataArray, SORTBY.ASC);9}
View Code
Array bubble sort, file read, database read, int array of string type converted to int array