Data: SHC 111U,SHB 22x,, SHA 5555G
I need Out Put:
SHB 22X, SHC 111U, SHA 5555G
Any Reference Document Please Send To me
Data: SHC 111U,SHB 22x,, SHA 5555G
I need Out Put:
SHB 22X, SHC 111U, SHA 5555G
Any Reference Document Please Send To me
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.
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.
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.
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);
}
}
Fantastic, well-optimized open source solution at http://dotnetperls.com/alphanumeric-sorting