Answered by:
StringBuilder sorting

Question
-
User-1256377279 posted
Hi Guys,
How to sort below log using Stringbuilder c#, Basically i want to sort via Row no:
Ref : Add MS 89010/17-21, Row no :8,Column name : Reference, Error message:Add MS 89010/17-21 is already in Iams
Ref : Add MS 89245/7, Row no :3,Column name : Languageofmaterial, Error message:LanguageOfMaterial is missing or column name is incorrect
Ref : Add MS 89245/7/1-6, Row no :4,Column name : Languageofmaterial, Error message:Englishs is an unknown LanguageOfMaterial term
Ref : Add MS 89245/7/1, Row no :5,Column name : Languageofmaterial, Error message:Arabi is an unknown LanguageOfMaterial term
Ref : Add MS 89245/7/2, Row no :6,Column name : Languagecodesofmaterial, Error message:ar is an unknown LanguageCodesOfMaterial term
Ref : Add MS 89245/7/7-15, Row no :7,Column name : Scriptcodesofmaterial, Error message:Ara is an unknown ScriptCodesOfMaterial termThanks,
Shabbir
Monday, January 14, 2019 4:09 PM
Answers
-
User-893317190 posted
Hi shabbir_215,
I do not understand why you want to sort the text using stringbuilder?
As far as I know , stringbuilder couldn't sort text.
If you only want to sort the rowno , you could refer to the code below.
// split the whole text using Ref: string[] allRows= File.ReadAllText(Server.MapPath("/csharpDemo/TextFile1.txt")).Split(new string[] { "Ref:" },StringSplitOptions.RemoveEmptyEntries);
// initialize a Regex object, it is used to get the Row no
Regex regex = new Regex(@"Row\s+no\s+:(\d+)");
// regex.Match will get a Match object , if you are not familiar with Regex,
// you only need to know regex.Match("Ref : Add MS 89010/17-21, Row no :8,Column name...").Greop[1].value could get the row number // Match match = regex.Match("Ref : Add MS 89010/17-21, Row no :8,Column name : Reference, Error message:Add MS 89010/17-21 is already in Iams "); // Group group = match.Groups[1]; string[] orderLine = allRows.OrderBy(line => // sort the string array using linq orderby, line here represents every line of your log Convert.ToInt32( (regex.Match(line).Groups[1].Value) // this will return the rownumber of every line )).ToArray(); foreach (var item in orderLine) // loop through the sorted lines to check the result { Response.Write("Ref:"+item+"<br/>"); } }The result.
To learn more about orderby in linq, you could refer to https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=netframework-4.7.2
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 15, 2019 2:25 AM
All replies
-
User-893317190 posted
Hi shabbir_215,
I do not understand why you want to sort the text using stringbuilder?
As far as I know , stringbuilder couldn't sort text.
If you only want to sort the rowno , you could refer to the code below.
// split the whole text using Ref: string[] allRows= File.ReadAllText(Server.MapPath("/csharpDemo/TextFile1.txt")).Split(new string[] { "Ref:" },StringSplitOptions.RemoveEmptyEntries);
// initialize a Regex object, it is used to get the Row no
Regex regex = new Regex(@"Row\s+no\s+:(\d+)");
// regex.Match will get a Match object , if you are not familiar with Regex,
// you only need to know regex.Match("Ref : Add MS 89010/17-21, Row no :8,Column name...").Greop[1].value could get the row number // Match match = regex.Match("Ref : Add MS 89010/17-21, Row no :8,Column name : Reference, Error message:Add MS 89010/17-21 is already in Iams "); // Group group = match.Groups[1]; string[] orderLine = allRows.OrderBy(line => // sort the string array using linq orderby, line here represents every line of your log Convert.ToInt32( (regex.Match(line).Groups[1].Value) // this will return the rownumber of every line )).ToArray(); foreach (var item in orderLine) // loop through the sorted lines to check the result { Response.Write("Ref:"+item+"<br/>"); } }The result.
To learn more about orderby in linq, you could refer to https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=netframework-4.7.2
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 15, 2019 2:25 AM -
User-1256377279 posted
Thank you so much, I know stringbuilder is not right way for sorting but there was unusual requirement.
Regards,
Shabbir
Tuesday, January 15, 2019 9:21 AM