i want to sort data from text file and save it on to another text file
here is my text file “employee.txt” i want to sort data using the “Employee code”
Employee code:107
First Name:swapnil
Last Name:dehjhja
Phone NO:6727672
Employee code:106
First Name:fhsgbf
Last Name:dehjhja
Phone NO:909888
Employee code:102
First Name:xyz
Last Name:dehjhja
Phone NO:098778
For the reading/writing of your files, you could simply use File.ReadAllLines to read the lines into a string array and File.WriteAllLines to write them back to a file. These functions will handle all of the opening/closing of the files so that you don’t have to.
To handle the sorting, we can use the orderby keyword in LINQ and a static helper function
GetEmployeeCodeto get the EmployeeCode. We can define our helper function as follows:The following code will then sort all of the lines in your file in ascending order by EmployeeCode and then write them to a new file: