How to sort number in alphanumeric

∥☆過路亽.° 提交于 2019-11-29 20:00:11

问题


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


回答1:


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




回答2:


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.




回答3:


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);
    }

}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/2619617/how-to-sort-number-in-alphanumeric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!