Home > other >  c# sorting by part of string
c# sorting by part of string

Time:07-12

I got a list of strings like:

{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,
}

i want to sort it OrderBy int after MMB in 5 column to received list like

   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,    

my code

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
}

I am thinking about sortin by Linq Orderby s => int.Parse or standard sort with IComparer

all suggestions are welcome. thanks everyone in advance !

CodePudding user response:

Given your list of items is in an enumerable called input you can use regex to pull out the value of MMB-**** and order by it

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

Live example: https://dotnetfiddle.net/rbbWXP

CodePudding user response:

Use Regex

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication23
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.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();

 

        }
    }
 
 
}
  • Related