c#按部分字符串排序



我得到了一个字符串列表,如:

{ID:    caa206a29   Product Stock is:   MMB-808     ->  Expected    Stock   on  MMB:    MMB-813,    
ID:    46ca37fbb   Product Stock is:   MMB-8002    ->  Expected    Stock   on  MMB:    MMB-222,    
ID:    e8109b18d   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223,    
ID:    1747fb04a   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223,    
ID:    ecbf00f93   Product Stock is:   MMB-8001    ->  Expected    Stock   on  MMB:    MMB-223,    
ID:    1b55eed17   Product Stock is:   MMB-868     ->  Expected    Stock   on  MMB:    MMB-864,    
ID:    a51fa3073   Product Stock is:   MMB-862     ->  Expected    Stock   on  MMB:    MMB-864,    
ID:    08b75eb96   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-864,
ID:    b89c8ac55   Product Stock is:   MMB-861     ->  Expected    Stock   on  MMB:    MMB-864,
ID:    70b709d1a   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-822,
}

我想在5列的MMB之后对其排序,以接收列表,如

ID:  caa206a29   Product Stock is:   MMB-808     ->  Expected    Stock   on  MMB:    MMB-813,    
ID:  b89c8ac55   Product Stock is:   MMB-861     ->  Expected    Stock   on  MMB:    MMB-864,
ID:  a51fa3073   Product Stock is:   MMB-862     ->  Expected    Stock   on  MMB:    MMB-864,    
ID:  08b75eb96   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-864,
ID:  70b709d1a   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-822,
ID:  1b55eed17   Product Stock is:   MMB-868     ->  Expected    Stock   on  MMB:    MMB-864,    
ID:  ecbf00f93   Product Stock is:   MMB-8001    ->  Expected    Stock   on  MMB:    MMB-223,    
ID:  46ca37fbb   Product Stock is:   MMB-8002    ->  Expected    Stock   on  MMB:    MMB-222,    
ID:  e8109b18d   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223,    
ID:  1747fb04a   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223,    

我的代码
string [] list_of_product = new string [];
var separator = string ["  "];
foreach(sting s in list_of_product)
{
var spliting = s.Split(separator, StringSplitOptions.None);

//sorting method
}

我正在考虑用Linq order排序s =>int。使用IComparer解析或标准排序

所有的建议都是受欢迎的。提前感谢大家!

给定您的项目列表在一个称为input的枚举中,您可以使用regex提取MMB-****的值并按其排序

var items = input.Select(x => new { 
Original = x, 
MMBVal = int.Parse(Regex.Match(x, "Product Stock is:   MMB-([0-9]+)").Groups[1].Value) 
});
foreach(var item in items.OrderBy(x => x.MMBVal))
{
Console.WriteLine(item.Original);
}

实例:https://dotnetfiddle.net/rbbWXP

使用正则表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
string[] inputs = {
"ID:    caa206a29   Product Stock is:   MMB-808     ->  Expected    Stock   on  MMB:    MMB-813",
"ID:    46ca37fbb   Product Stock is:   MMB-8002    ->  Expected    Stock   on  MMB:    MMB-222",    
"ID:    e8109b18d   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223",    
"ID:    1747fb04a   Product Stock is:   MMB-8005    ->  Expected    Stock   on  MMB:    MMB-223",    
"ID:    ecbf00f93   Product Stock is:   MMB-8001    ->  Expected    Stock   on  MMB:    MMB-223",    
"ID:    1b55eed17   Product Stock is:   MMB-868     ->  Expected    Stock   on  MMB:    MMB-864",    
"ID:    a51fa3073   Product Stock is:   MMB-862     ->  Expected    Stock   on  MMB:    MMB-864",    
"ID:    08b75eb96   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-864",
"ID:    b89c8ac55   Product Stock is:   MMB-861     ->  Expected    Stock   on  MMB:    MMB-864",
"ID:    70b709d1a   Product Stock is:   MMB-863     ->  Expected    Stock   on  MMB:    MMB-822"
};
string pattern = @"MMB-(?'mmb'd+)";
string[] output = inputs.OrderBy(x => int.Parse(Regex.Match(x, pattern).Groups["mmb"].Value)).ToArray();

}
}


}

相关内容

  • 没有找到相关文章

最新更新