drop
drop

Reputation: 61

How to sort number in alphanumeric

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

Upvotes: 6

Views: 9290

Answers (5)

sblom
sblom

Reputation: 27343

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

Upvotes: 5

Felan
Felan

Reputation: 1273

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

}

Upvotes: 1

Robert Hardy
Robert Hardy

Reputation: 403

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.

Upvotes: 0

msarchet
msarchet

Reputation: 15242

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.

Upvotes: 0

Dean Harding
Dean Harding

Reputation: 72658

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.

Upvotes: 4

Related Questions