Topic: First of all, you need to implement a function: a custom function for comparing the size of two strings (c#/java system functions are not allowed); then sort a string data in ascending order (when using string size comparison during the sorting process, using a custom string size comparison function).
Here's what I've come up with personally:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { /** * Compare the size of two strings (without using the internal comparison function of c#/java), sort in ascending order */ static void Main(string[] args) { string[] strArr = new string[] { "a", "a2", "bc", "a1" }; for (int i = 0; i < strArr.Length - 1; i++) { for (int j = i + 1; j < strArr.Length; j++) { string temp = ""; if (Compare(strArr[i], strArr[j]) < 0) { temp = strArr[i]; strArr[i] = strArr[j]; strArr[j] = temp; } } } foreach (string str in strArr) { Console.WriteLine(str); } Console.ReadKey(); } static int Compare(string a, string b) { // If a=b=null, return a is equal to b if (a == null && b == null ) return 0 ; // If a!=null and b=null, return a is greater than b if (a != null && b == null ) return 1 ; // If a==null and b!=null, return a is less than b if (a == null && b != null ) return - 1 ; char [] aArray = a.ToCharArray (); char [] bArray = b.ToCharArray (); // otherwise compare them directly for ( int i = 0 ; i < aArray.Length; i++ ) { if (i > bArray.Length - 1) return 1; if (aArray[i] > bArray[i]) { return 1; } else if (aArray [i] < bArray [i]) { return -1; } // If equal, do not process. . . } // 此时则a.length==b.length或者b.length>a.length return (a.Length == b.Length) ? 0 : -1; } } }