Click here to Skip to main content
16,016,570 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to remove the new line from the end of string ?This is my code to sort the records after writing it with binarywriter

What I have tried:

string inFile = Class1.filename;
       string outFile = Class1.filename;

       var contents = File.ReadAllLines(inFile);
      Array.Sort(contents);
       File.WriteAllLines(outFile, contents);
Posted
Updated 27-Apr-17 23:11pm
v2
Comments
PIEBALDconsult 27-Apr-17 19:23pm    
Trim?
But ReadAllLines should have already removed them, so why do you think you need to?
Dave Kreskowiak 29-Apr-17 11:33am    
Because he's writing the text files with a Binary writer.

1 solution

ReadAllLines does remove all newlines, but WriteAllLines puts them all back!
Instead try this:
string inFile = Class1.filename;
string outFile = Class1.filename;
var contents = File.ReadAllLines(inFile);
Array.Sort(contents);      
File.WriteAllText(outFile, string.Concat(contents));
 
Share this answer
 
Comments
Member 13151067 28-Apr-17 4:10am    
when i try to use WriteAllText(outfile,string.concat(contents)); Records won't be sorted like writeAlllines
OriginalGriff 28-Apr-17 4:26am    
Yes they will.
You sorted them in the line before the write operation.
Member 13151067 28-Apr-17 4:47am    
How can i sort them?
OriginalGriff 28-Apr-17 4:52am    
What do you think this line does?

Array.Sort(contents);
Member 13151067 28-Apr-17 4:58am    
Sort the array of strings

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900