Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To expand on other people's suggestions:</p> <ol> <li>The hardest part of this is probably the code that transforms a number to an english string representing that number - Sani's programming assignment. I've included a simplified example that may not meet your requirements.</li> </ol> <pre>private static string[] digitnames = new string[] { "oh", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; private static string ReplaceDigits(string s) { string convertedSoFar = ""; //could use a StringBuilder if performance is an issue. for (int charPos = 0; charPos &lt; s.Length; charPos++) { if (char.IsNumber(s[charPos])) { //Add the digit name matching the digit. convertedSoFar += digitnames[int.Parse(s[charPos].ToString())]; } else { //we've reached the end of the numbers at the front of the string. //Add back the rest of s and quit. convertedSoFar += s.Substring(charPos); break; } } return convertedSoFar; }</pre> <p>This code turns "101 dalmations" into "oneohone dalmations" and "12 angry men" into "onetwo angry men". A more complete solution could be built, perhaps from <a href="https://stackoverflow.com/questions/3213/c-convert-integers-into-written-numbers">Wedge's solution to a slightly different problem</a>. I haven't tested that code, and it isn't designed to handle the string after the digits, but it's probably a good start.</p> <ol start="2"> <li>In modern C# (3.0 and higher, I think) you can pass a method name to Sort, rather than explicitly creating an IComparable or IComparer. This is essentially the same idea as Michael's link. Another related option is an anonymous <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" rel="nofollow noreferrer">lambda expression</a> which wouldn't even require an external method. Personally I think the code reads more cleanly this way.</li> </ol> <pre>private static int NumberReplacingCompare(string strA, string strB) { return ReplaceDigits(strA).CompareTo(ReplaceDigits(strB)); } private static void OutputSortedStrings() { List strings = new List(File.ReadAllLines(@"D:\Working\MyStrings.txt")); //pull the strings from a file (or wherever they come from strings.Sort(NumberReplacingCompare); //sort, using NumberReplacingCompare as the comparison function foreach (string s in strings) { System.Console.WriteLine(s); } }</pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload