Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:33:24+00:00 2026-06-05T04:33:24+00:00

i want to sort data from text file and save it on to another

  • 0

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


  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:33:26+00:00Added an answer on June 5, 2026 at 4:33 am

    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 GetEmployeeCode to get the EmployeeCode. We can define our helper function as follows:

    public static int GetEmployeeCode(string line)
    {
        // Get the substring starting after "Employee code:"
        // ... and stopping at the first space.
        string employeeCode = line.Substring(14).Split(' ')[0];
        int code;
        Int32.TryParse(employeeCode, out code);
        return code;
    }
    

    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:

    string[] lines = File.ReadAllLines(@"C:\original.txt");
    
    // Sort the rows in lines by EmployeeCode
    lines = (from line in lines
            orderby GetEmployeeCode(line) ascending
            select line).ToArray();
    
    File.WriteAllLines(@"C:\sorted.txt", lines);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to read from an unsorted source text file (one
I have problems getting data from grails using extjs. My Grails controller code is:
I have a list of data from SQL field. I want to sort by
I'm having some trouble sorting data from a text file by a certain field.
I want to be able to sort the data after adding a model to
I have a log file named a.log ! I want sort it by the
Can anyone recommend a fast way to sort the contents of a text file,
What I'm looking to do is split data from string into an array. Here's
I am trying to sort data from different tables by its time.. I have
I have 3 list boxes. I am splitting a text file to sort the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.