按字母顺序对字符串中的数字进行排序

发布于 2024-08-23 01:04:46 字数 122 浏览 10 评论 0原文

我们有一个标题列表,其中一些以数字开头(例如 5 Ways to Make Widgets)。我们希望将其排序为“五种方式......”而不更改标题。我们知道有些电影场所会这样做,但我在网上找不到有关如何操作的信息。有什么想法吗?

We have a list of titles, some of which start with numbers (e.g. 5 Ways to Make Widgets). 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺渁∝ 2024-08-30 01:04:47

存储原始标题和拼写的标题。

select OriginalTitle from Movies order by spelledTitle

另请参阅:将整数转换为书面数字

Store both the original title and the spelled-out title.

select OriginalTitle from Movies order by spelledTitle

See also: Convert integers to written numbers

蝶…霜飞 2024-08-30 01:04:47

在计算机科学中,学习编程时,有时会有一个将数字转换为文本的作业。喜欢:

526 = Fivehundredtwentysix

在这种情况下,这可能是您需要的。

这是一项微不足道的作业,但却是一个很好的教训。

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.

等待我真够勒 2024-08-30 01:04:47

创建自定义比较器请参阅 http://support.microsoft.com/kb/320727

本质上,您想要做的是测试第一个字符是否是数字。如果它不只是恢复为标准字符串比较,如果是,那么您可以进行一些额外的处理来获取数字的文本版本。

一旦你拥有了这种最排序算法,就可以让你传递比较器。

Create a custom comparer see 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.

始终不够 2024-08-30 01:04:47

扩展其他人的建议:

  1. 其中最难的部分可能是将数字转换为表示该数字的英文字符串的代码 - Sani 的编程作业。我提供了一个可能无法满足您的要求的简化示例。
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;
}

此代码将“101 dalmations”转换为“oneohone dalmations”,将“12angry men”转换为“onetwoangry men”。可以构建更完整的解决方案,也许来自Wedge 针对略有不同的问题的解决方案。我还没有测试过该代码,它也不是设计来处理数字后面的字符串的,但这可能是一个好的开始。

  1. 在现代 C#(我认为是 3.0 及更高版本)中,您可以将方法名称传递给 Sort,而不是显式创建 IComparable 或 IComparer。这与迈克尔的链接本质上是相同的想法。另一个相关选项是匿名 lambda 表达式,它甚至不需要一个外部方法。我个人认为这样代码读起来更干净。
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);
    }
}

To expand on other people's suggestions:

  1. 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.
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". A more complete solution could be built, perhaps from Wedge's solution to a slightly different problem. 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.

  1. 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 lambda expression which wouldn't even require an external method. Personally I think the code reads more cleanly this way.
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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文