如何按字母数字对数字进行排序

发布于 2024-08-28 16:04:06 字数 202 浏览 6 评论 0原文

输入:

SHC 111U、SHB 22x、SHA 5555G

所需输出:

SHB 22X、SHC 111U、SHA 5555G

我只需对停车区中的车辆编号进行排序,而不是前缀和后缀字母

Input:

SHC 111U,SHB 22x,, SHA 5555G

Needed output:

SHB 22X, SHC 111U, SHA 5555G

I have to sort only Vehicle no in the Parking Area not prefix and suffix letter

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

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

发布评论

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

评论(5

生活了然无味 2024-09-04 16:04:06

出色的、优化良好的开源解决方案,位于 http://dotnetperls.com/alphanumeric-sorting

Fantastic, well-optimized open source solution at http://dotnetperls.com/alphanumeric-sorting

窝囊感情。 2024-09-04 16:04:06

没有任何内置功能可以执行此操作,但您可以通过首先提取数字并基于此进行排序来完成此操作。例如:

class VehicleNumberComparer : IComparer<string>
{
    public int Compare(string lhs, string rhs)
    {
        var numExtract = new Regex("[0-9]+");
        int lhsNumber = int.Parse(numExtract.Match(lhs).Value);
        int rhsNumber = int.Parse(numExtract.Match(rhs).Value);
        return lhsNumber.CompareTo(rhsNumber);
    }
}

这是未经测试的(并且可能在不修改的情况下甚至无法编译),没有错误检查,并且可能不是世界上最快的方法,但应该给您一个想法。

There is nothing built-in to do this, but you can do it by first extracting the numbers and sorting based on that. For example:

class VehicleNumberComparer : IComparer<string>
{
    public int Compare(string lhs, string rhs)
    {
        var numExtract = new Regex("[0-9]+");
        int lhsNumber = int.Parse(numExtract.Match(lhs).Value);
        int rhsNumber = int.Parse(numExtract.Match(rhs).Value);
        return lhsNumber.CompareTo(rhsNumber);
    }
}

This is untested (and probably won't even compile without modification), has no error checking, and probably isn't the fastest method in the world, but should give you an idea.

反话 2024-09-04 16:04:06

如果可能有没有号码的车牌,那么您应该检查一下。

static int SortPlate(string plate)
{
    int plateNumber;
    Regex regex = new Regex(@"\d+");
    Int32.TryParse(regex.Match(plate).Value, out plateNumber);

    return plateNumber;
}

static void Main(string[] args)
{
    IEnumerable<string> data = new List<string>() {"SHC 111U", "SHB 22x", "SHA 5555G", "HOT STUFF"};

    var sortedList = from z in data
                     orderby SortPlate(z)
                     select z;

    foreach (string plate in sortedList)
    {
        Console.WriteLine(plate);
    }

}

如果这是绝对不可能的,并且世界末日会在没有数字的盘子出现之前到来,那么这个缩写形式将起作用:

static void Main(string[] args)
{
    IEnumerable<string> data = new List<string>() {"SHC 111U", "SHB 22x", "SHA 5555G"};

    Regex regex = new Regex(@"\d+");
    var sortedList = from z in data
                     orderby Int32.Parse(regex.Match(z).Value)
                     select z;

    foreach (string plate in sortedList)
    {
        Console.WriteLine(plate);
    }

}

If it is possible to have a plate without a number then you should check for that.

static int SortPlate(string plate)
{
    int plateNumber;
    Regex regex = new Regex(@"\d+");
    Int32.TryParse(regex.Match(plate).Value, out plateNumber);

    return plateNumber;
}

static void Main(string[] args)
{
    IEnumerable<string> data = new List<string>() {"SHC 111U", "SHB 22x", "SHA 5555G", "HOT STUFF"};

    var sortedList = from z in data
                     orderby SortPlate(z)
                     select z;

    foreach (string plate in sortedList)
    {
        Console.WriteLine(plate);
    }

}

If it is absolutely impossible and the end of the world would come before there could ever be a plate without numbers then this shortened form will work:

static void Main(string[] args)
{
    IEnumerable<string> data = new List<string>() {"SHC 111U", "SHB 22x", "SHA 5555G"};

    Regex regex = new Regex(@"\d+");
    var sortedList = from z in data
                     orderby Int32.Parse(regex.Match(z).Value)
                     select z;

    foreach (string plate in sortedList)
    {
        Console.WriteLine(plate);
    }

}
快乐很简单 2024-09-04 16:04:06

执行此操作的一个好方法是执行类似的操作

编写一个正则表达式以仅匹配名称的数字部分,将其放入配对整数值的集合中,第一个是从字符串中提取的数字,第二个是从字符串中提取的数字是原始列表中数字的索引。然后对第二个列表进行排序,然后使用集合中的第二个数字对第一个列表重新排序。

A good way to do this would be to do something like this

Write a regular expression to match just the numeric portion of the name, put that in a collection of paired integer values, the first being the number you pulled from your string and the second being the index of the number in the original list. Then sort the second list, and then reorder the first list using the second number in your collection.

﹏雨一样淡蓝的深情 2024-09-04 16:04:06

Use a Sort method that accepts an IComparer object and pass it your collection of vehicle numbers. You will need to define a custom class that implements IComparer. In the Compare method of that class you can write code to compare the two vehicle numbers. You should probably use a regex for extracting the numerical part of the vehicle number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文