C# Read text file and sorting it in an array

14
2



This Tutorial helps to learn Read data’s from text file after to convert to array then sort records ascending or descending as well as you can set starting and ending ranges

Nguồn:https://wijstaanvooronzegrondrechten.org/

14 COMMENTS

  1. Is this channel still active? How would you code this problem?
    1.) Create a C# program that asks the user for 10 real numbers then computes and displays the average of the numbers

  2. Dear sir
    can you provide the c code to split the letter by letter in a given string
    for example if hello means i want to split h e l l o like this way please provide me thanks in advance!!!!!!!!!!!!!

  3. Your a genius, I needed to read a file and display it has an int and also double, and then sort the data using sorting algorithms as part of my assignment. Thank you so much.

  4. There is many ways to sort lines from a text file, without coding too much this is a simple way to do it using linq:

    Get the text file contents and pass them into a List<string>:

    List<string> listofContents = File.ReadAllLines(FileName).Where(x => !string.IsNullOrEmpty(x.Trim()) && !string.IsNullOrWhiteSpace(x.Trim())).ToList();

    SORT THE CONTENTS:

    listofContents.Sort();

    If you want to overwrite immediately your text file:

    File.WriteAllLines(FileName, listofContents.Where(x => !string.IsNullOrEmpty(x.Trim()) && !string.IsNullOrWhiteSpace(x.Trim())).ToArray());

    If you want to put the content already sorted anywhere:

    String[] MySortedContent = listofContents.Where(x => !string.IsNullOrEmpty(x.Trim()) && !string.IsNullOrWhiteSpace(x.Trim())).ToArray()

    Then just need a loop to place the content into your desired control, in your case into a ListBox:
    (You may need to clean your ListBox content before passing the new content)

    for (int i = 0; i < MySortedContent.Lenght; i++ )
    {
    listBox2.Items.Add(MySortedContent[i]);
    }

    At the end you just need and want to sort, you don't need to play.

    Best regards.

    Carlos Metafunken.

LEAVE A REPLY

Please enter your comment!
Please enter your name here