locked
OrderBy Array using Lambda Expression RRS feed

  • Question

  • User-1256377279 posted

    Hi All,

    I am referring my previous post which worked initially but due to other format it failed to do descending order

    https://forums.asp.net/t/2151397.aspx?StringBuilder+sorting+ 

     string[] allRows = err.Split(new string[] { "Ref : " }, StringSplitOptions.RemoveEmptyEntries);
    
                Regex rex = new Regex(@"Row\s+no\s+:(\d+)");
                //Regex rex = new Regex(@"Row\s+no\s+:([0-9]*)");
    
                string[] orderLine = allRows.OrderByDescending(line => rex.Match(line).Groups[1].Value.Length).ThenByDescending(c => c).ToArray();

    Below is my output :( 


    Start Time 02 - 04 - 2019 15: 24:23

    Ref : New Migrator Date format test 1/9, Row no :10,Column name : Reference, Error message:New Migrator Date format test 1/9 is already in Iams Loaded
    Ref : New Migrator Date format test 1/44, Row no :42,Column name : Reference, Error message:New Migrator Date format test 1/44 is already in Iams Loaded
    Ref : New Migrator Date format test 1/43, Row no :41,Column name : Reference, Error message:New Migrator Date format test 1/43 is already in Iams Loaded
    Ref : New Migrator Date format test 1/42, Row no :40,Column name : Reference, Error message:New Migrator Date format test 1/42 is already in Iams Loaded
    Ref : New Migrator Date format test 1/41, Row no :39,Column name : Reference, Error message:New Migrator Date format test 1/41 is already in Iams Loaded
    Ref : New Migrator Date format test 1/40, Row no :38,Column name : Reference, Error message:New Migrator Date format test 1/40 is already in Iams Loaded
    Ref : New Migrator Date format test 1/39, Row no :37,Column name : Reference, Error message:New Migrator Date format test 1/39 is already in Iams Loaded
    Ref : New Migrator Date format test 1/38, Row no :36,Column name : Reference, Error message:New Migrator Date format test 1/38 is already in Iams Loaded
    Ref : New Migrator Date format test 1/37, Row no :35,Column name : Reference, Error message:New Migrator Date format test 1/37 is already in Iams Loaded
    Ref : New Migrator Date format test 1/36, Row no :34,Column name : Reference, Error message:New Migrator Date format test 1/36 is already in Iams Loaded
    Ref : New Migrator Date format test 1/35, Row no :33,Column name : Reference, Error message:New Migrator Date format test 1/35 is already in Iams Loaded
    Ref : New Migrator Date format test 1/34, Row no :32,Column name : Reference, Error message:New Migrator Date format test 1/34 is already in Iams Loaded
    Ref : New Migrator Date format test 1/33, Row no :31,Column name : Reference, Error message:New Migrator Date format test 1/33 is already in Iams Loaded
    Ref : New Migrator Date format test 1/32, Row no :30,Column name : Reference, Error message:New Migrator Date format test 1/32 is already in Iams Loaded
    Ref : New Migrator Date format test 1/31, Row no :29,Column name : Reference, Error message:New Migrator Date format test 1/31 is already in Iams Loaded
    Ref : New Migrator Date format test 1/30, Row no :28,Column name : Reference, Error message:New Migrator Date format test 1/30 is already in Iams Loaded
    Ref : New Migrator Date format test 1/29, Row no :27,Column name : Reference, Error message:New Migrator Date format test 1/29 is already in Iams Loaded
    Ref : New Migrator Date format test 1/28, Row no :26,Column name : Reference, Error message:New Migrator Date format test 1/28 is already in Iams Loaded
    Ref : New Migrator Date format test 1/27, Row no :25,Column name : Reference, Error message:New Migrator Date format test 1/27 is already in Iams Loaded
    Ref : New Migrator Date format test 1/25, Row no :24,Column name : Reference, Error message:New Migrator Date format test 1/25 is already in Iams Loaded
    Ref : New Migrator Date format test 1/23, Row no :23,Column name : Related persons, Error message: 047 prefix not in the ID 045-001718988=Creator
    Ref : New Migrator Date format test 1/23, Row no :23,Column name : Reference, Error message:New Migrator Date format test 1/23 is already in Iams Loaded
    Ref : New Migrator Date format test 1/22, Row no :22,Column name : Related persons, Error message: 047 prefix not in the ID 045-001718988=Creator
    Ref : New Migrator Date format test 1/22, Row no :22,Column name : Reference, Error message:New Migrator Date format test 1/22 is already in Iams Loaded
    Ref : New Migrator Date format test 1/21, Row no :21,Column name : Related persons, Error message: 047 prefix not in the ID 045-001718988=Creator
    Ref : New Migrator Date format test 1/21, Row no :21,Column name : Reference, Error message:New Migrator Date format test 1/21 is already in Iams Loaded
    Ref : New Migrator Date format test 1/20, Row no :20,Column name : Related persons, Error message: 047 prefix not in the ID 045-001718988=Creator
    Ref : New Migrator Date format test 1/20, Row no :20,Column name : Reference, Error message:New Migrator Date format test 1/20 is already in Iams Loaded
    Ref : New Migrator Date format test 1/19, Row no :19,Column name : Reference, Error message:New Migrator Date format test 1/19 is already in Iams Loaded
    Ref : New Migrator Date format test 1/17, Row no :18,Column name : Reference, Error message:New Migrator Date format test 1/17 is already in Iams Loaded
    Ref : New Migrator Date format test 1/16, Row no :17,Column name : Reference, Error message:New Migrator Date format test 1/16 is already in Iams Loaded
    Ref : New Migrator Date format test 1/15, Row no :16,Column name : Reference, Error message:New Migrator Date format test 1/15 is already in Iams Loaded
    Ref : New Migrator Date format test 1/14, Row no :15,Column name : Reference, Error message:New Migrator Date format test 1/14 is already in Iams Loaded
    Ref : New Migrator Date format test 1/13, Row no :14,Column name : Reference, Error message:New Migrator Date format test 1/13 is already in Iams Loaded
    Ref : New Migrator Date format test 1/12, Row no :13,Column name : Reference, Error message:New Migrator Date format test 1/12 is already in Iams Loaded
    Ref : New Migrator Date format test 1/11, Row no :12,Column name : Reference, Error message:New Migrator Date format test 1/11 is already in Iams Loaded
    Ref : New Migrator Date format test 1/10, Row no :11,Column name : Reference, Error message:New Migrator Date format test 1/10 is already in Iams Loaded
    Ref : New Migrator Date format test 1/8, Row no :9,Column name : Reference, Error message:New Migrator Date format test 1/8 is already in Iams Loaded
    Ref : New Migrator Date format test 1/7, Row no :8,Column name : Related persons, Error message: 047 prefix not in the ID 045-001718988=Creator
    Ref : New Migrator Date format test 1/7, Row no :8,Column name : Reference, Error message:New Migrator Date format test 1/7 is already in Iams Loaded
    Ref : New Migrator Date format test 1/6, Row no :7,Column name : Reference, Error message:New Migrator Date format test 1/6 is already in Iams Loaded
    Ref : New Migrator Date format test 1/5, Row no :6,Column name : Reference, Error message:New Migrator Date format test 1/5 is already in Iams Loaded
    Ref : New Migrator Date format test 1/4, Row no :5,Column name : Reference, Error message:New Migrator Date format test 1/4 is already in Iams Loaded
    Ref : New Migrator Date format test 1/3, Row no :4,Column name : Reference, Error message:New Migrator Date format test 1/3 is already in Iams Loaded
    Ref : New Migrator Date format test 1/2, Row no :3,Column name : Reference, Error message:New Migrator Date format test 1/2 is already in Iams Loaded
    Ref : New Migrator Date format test 1/1, Row no :2,Column name : Reference, Error message:New Migrator Date format test 1/1 is already in Iams Loaded

    End Time 02 - 04 - 2019 15: 24:30

    ****************************************************************************************************************************************************

    Tuesday, April 2, 2019 3:31 PM

Answers

  • User-1256377279 posted

    Find the solution for other to refer

         public string OrderErrorLog(string err)
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder eb = new StringBuilder();
    
                string[] allRows = err.Split(new string[] { "Ref : " }, StringSplitOptions.RemoveEmptyEntries);
    
                Regex rex = new Regex(@"Row\s+no\s+:(\d+)");
                //Regex rex = new Regex(@"Row\s+no\s+:([0-9]*)");
    
    
                var orderLine = from i in allRows
                          let value = rex.Match(i).Groups[1].Value
                          let numval = value == "" ? 0 : int.Parse(value)
                          orderby numval descending
                          select i;
    
                //string[] orderLine = allRows.OrderByDescending(line => rex.Match(line).Groups[1].Value.Length).ThenByDescending(c => c).ToArray();
    
                
                foreach (var item in orderLine)
                {
                    if (item.Contains("End Time"))
                    {
                        EndTime = item;
                    }
                    else if (item.Contains("Start Time"))
                    {
                        string[] allitem = item.Split(new string[] { "\t\r\nStart Time " }, StringSplitOptions.RemoveEmptyEntries);
                        StartTime = "\t\r\nStart Time " + allitem[1].ToString();
                        sb.Append("Ref : " + allitem[0].ToString() + "");                   
                    }
                    else
                    {
                        sb.Append("Ref : " + item + "");                   
                    }
    
                }
    
                eb.Append(StartTime);
                eb.Append("\t\r\n\t\r\n");
                eb.Append(sb.ToString());
                eb.Append(EndTime);
                eb.Append("****************************************************************************************************************************************************");
    
                return eb.ToString();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2019 8:39 AM

All replies

  • User1724605321 posted

    Hi shabbir_215 ,

    What do you mean by " due to other format " , please explain more about that  .

    Best Regards,

    Nan Yu

    Wednesday, April 3, 2019 3:01 AM
  • User-1256377279 posted

    Find the solution for other to refer

         public string OrderErrorLog(string err)
            {
                StringBuilder sb = new StringBuilder();
                StringBuilder eb = new StringBuilder();
    
                string[] allRows = err.Split(new string[] { "Ref : " }, StringSplitOptions.RemoveEmptyEntries);
    
                Regex rex = new Regex(@"Row\s+no\s+:(\d+)");
                //Regex rex = new Regex(@"Row\s+no\s+:([0-9]*)");
    
    
                var orderLine = from i in allRows
                          let value = rex.Match(i).Groups[1].Value
                          let numval = value == "" ? 0 : int.Parse(value)
                          orderby numval descending
                          select i;
    
                //string[] orderLine = allRows.OrderByDescending(line => rex.Match(line).Groups[1].Value.Length).ThenByDescending(c => c).ToArray();
    
                
                foreach (var item in orderLine)
                {
                    if (item.Contains("End Time"))
                    {
                        EndTime = item;
                    }
                    else if (item.Contains("Start Time"))
                    {
                        string[] allitem = item.Split(new string[] { "\t\r\nStart Time " }, StringSplitOptions.RemoveEmptyEntries);
                        StartTime = "\t\r\nStart Time " + allitem[1].ToString();
                        sb.Append("Ref : " + allitem[0].ToString() + "");                   
                    }
                    else
                    {
                        sb.Append("Ref : " + item + "");                   
                    }
    
                }
    
                eb.Append(StartTime);
                eb.Append("\t\r\n\t\r\n");
                eb.Append(sb.ToString());
                eb.Append(EndTime);
                eb.Append("****************************************************************************************************************************************************");
    
                return eb.ToString();
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 8, 2019 8:39 AM