[英]Sorting digits in a string alphabetically
We have a list of titles, some of which start with numbers (eg 5 Ways to Make Widgets). 我们有一个标题列表,其中一些以数字开头(例如5种制作小部件的方法)。 We would like to sort this as if it were "Five Ways..." without changing the title. 我们希望将其排序为“五种方式...”,而无需更改标题。 We know that some movie places do this, but I can't find info online on how to do it. 我们知道有些电影院会这样做,但是我无法在线找到有关如何做的信息。 Any ideas? 有任何想法吗?
Store both the original title and the spelled-out title. 存储原始标题和拼写出的标题。
select OriginalTitle from Movies order by spelledTitle
See also: Convert integers to written numbers 另请参阅: 将整数转换为书面数字
In Computer Science, when learning programming, sometimes there is an assignment to convert numbers to text. 在计算机科学中,学习编程时,有时会分配一个将数字转换为文本的任务。 Like: 喜欢:
526 = Fivehundredtwentysix
This is probably something you'll need in this case. 在这种情况下,这可能是您需要的。
It is a trivial assignment but it is a good lesson. 这是一项琐碎的任务,但这是一个很好的教训。
Create a custom comparer see http://support.microsoft.com/kb/320727 . 创建自定义比较器,请参见http://support.microsoft.com/kb/320727 。
Essentially what you want to do is test if the first character is a number. 本质上,您要测试的是第一个字符是否为数字。 If it isn't just revert to a standard string compare, if it is then you can do some additional processing to get a textual version of the number. 如果不只是还原为标准字符串比较,如果是,则可以进行一些附加处理以获取数字的文本版本。
Once you have this most sort algorithms will allow you to pass the comparer in. 一旦拥有了这种大多数排序算法,您就可以将比较器传入。
To expand on other people's suggestions: 扩展他人的建议:
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 < 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; }
This code turns "101 dalmations" into "oneohone dalmations" and "12 angry men" into "onetwo angry men". 该代码将“ 101 dalmations”转换为“ oneohone dalmations”,将“ 12生气的人”转换为“ onetwo愤怒的人”。 A more complete solution could be built, perhaps from Wedge's solution to a slightly different problem . 可以构建更完整的解决方案,也许是从Wedge的解决方案到稍微不同的问题 。 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. 我尚未测试过该代码,并且该代码并非旨在处理数字后的字符串,但这可能是一个不错的开始。
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); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.