How to sort number in alphanumeric

前端 未结 5 387
失恋的感觉
失恋的感觉 2021-01-03 01:16

Input:

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

Needed output:

SHB 22X, SHC 111U, SHA 5555G

I

相关标签:
5条回答
  • 2021-01-03 01:18

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

    0 讨论(0)
  • 2021-01-03 01:25

    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.

    0 讨论(0)
  • 2021-01-03 01:33

    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.

    0 讨论(0)
  • 2021-01-03 01:35

    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.

    0 讨论(0)
  • 2021-01-03 01:41

    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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题