c# - Sorting some records in the .txt file without disturbing other records and overwriting in the same file -
i trying read text file line line , trying sort "puma" named shoes in ascending manner according price , save changes in same file without disturbing order of "nike" , "adidas" shoes. great if can me in this. in advance.
here code below tried
public class program { static string content; static string outputfile = @"c:\users\desktop\shoerack.txt"; public static void main(string[]args) { sorting(); } public static void sorting() { try { string[] scores =system.io.file.readalllines(@"c:\users\desktop\shoerack.txt"); var numbers = scores.orderby(x=>(x.split(',')[2])); foreach(var dat in numbers) { content = dat.tostring(); writetofile(outputfile,content); } } catch(exception e) { console.writeline(e); console.readline(); } } public static void writetofile(string outputfile, string content) { using(system.io.streamwriter file = new system.io.streamwriter(@outputfile)) { file.writeline(content); } } }
the text file below named shoerack.txt
shoecompany,price,size,color,avaibility nike,75,6,red,yes nike,80,5,yellow,yes nike,50,9,white,yes nike,44,5,white,no adidas,50,7,green,yes adidas,55,8,grey,yes adidas,40,5,red,yes puma,50,7,red,yes puma,55,6,yellow,yes puma,44,5,red,yes puma,45,4,green,no
output is
shoecompany,price,size,color,avaibility nike,75,6,red,yes nike,80,5,yellow,yes nike,50,9,white,yes nike,44,5,white,no adidas,50,7,green,yes adidas,55,8,grey,yes adidas,40,5,red,yes puma,50,7,red,yes puma,55,6,yellow,yes puma,44,5,red,yes puma,45,4,green,no shoecompany,price,size,color,avaibility nike,75,6,red,yes nike,80,5,yellow,yes nike,50,9,white,yes nike,44,5,white,no adidas,50,7,green,yes adidas,55,8,grey,yes adidas,40,5,red,yes puma,50,7,red,yes puma,55,6,yellow,yes puma,44,5,red,yes puma,45,4,green,no
expected output is
shoecompany,price,size,color,avaibility nike,75,6,red,yes nike,80,5,yellow,yes nike,50,9,white,yes nike,44,5,white,no adidas,50,7,green,yes adidas,55,8,grey,yes adidas,40,5,red,yes puma,45,4,green,no puma,44,5,red,yes puma,55,6,yellow,yes puma,50,7,red,yes
what code not doing checking see if string puma
.
static string content; static string outputfile = @"c:\users\desktop\shoerack.txt"; public static void sorting() { try { string[] scores = system.io.file.readalllines(@"c:\users\desktop\shoerack.txt"); //split 2 lists: 1 puma, 1 others list<string> pumaentries = new list<string>(); list<string> otherentries = new list<string>(); foreach (string item in scores) { if (item.split(',')[0].toupper() == "puma") { pumaentries.add(item); } else { otherentries.add(item); } } //now sort puma entries var sorted = pumaentries.orderby(x => x.split(',')[1]); //now output "other" entries, puma ones using (system.io.streamwriter file = new system.io.streamwriter(@outputfile)) { foreach (string dat in otherentries) { file.writeline(dat); } foreach (string dat in sorted) { file.writeline(dat); } } } catch (exception e) { console.writeline(e); console.readline(); } }
this code splits puma entries separate list sorting, , keeps others unchanged.
note string array zero-based, to sort price, need sort [1]
not [2]
.
Comments
Post a Comment