I created a text file using following method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;





namespace FajlliTekstual
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fileStream = new FileStream("shenime.txt", FileMode.Create, FileAccess.Write);
            TextWriter writer = new StreamWriter(fileStream);
            Console.WriteLine("Someone Else");
            writer.WriteLine("Bla Bla");
            writer.WriteLine("Bla Bla");
            writer.WriteLine("Unkown Person");
            writer.Close();
            fileStream.Close();
            Console.ReadLine();
        }
    }
}

**/Now the text file holds the first and last name of someone.
My goal is to read this text file and to show the results in a console application sorted name like those that begin with A or B or other letter.
This first name latter should be given by the user and then the list must be shown.
Thank you in advance for your reply.
Cheers.
/
**

Recommended Answers

All 8 Replies

I don't find what your trying to do clear. Nor is the example data any good. Do Bla Bla count as an individual? or information relating to Someone else? Please consider re-doing the explanation with clarified requirements.

Ok the BLA BLA and Someone else are the first and last names of someone. It means that in a text file I created is a list of First and Last names. All i want to know is how to create a method to sort the list with those names that begin with a specific letter for example with letter D. The result should be shown in console application.
Hopefully you got the point of my goal.

So say I had this as my list

Steve A
Steve B
John C
Scott A
Dani T

and wanted the letter S, your output should be:

Scott A
Steve A
Steve B

Correct?

Yes, that's correct.

One possible solution....

string[] sortit = File.ReadAllLines("shenime.txt");
Array.Sort(sortit);
foreach (string str in sortit) Console.WriteLine(str); 
            List<string> Names = new List<string>();
            List<string> SortedRefinedNames = new List<string>();
            StreamReader sr = new StreamReader(@"C:\Users\maskew\Desktop\Names.txt");
            string NameEntry;
            while ((NameEntry = sr.ReadLine()) != null)
            {
                Names.Add(NameEntry);
            }
            sr.Close();

            Console.Write("First letter of name to search: ");
            string Letter = Console.ReadLine();

            foreach (string Name in Names)
            {
                if (Name.Substring(0, 1).ToUpper() == Letter.ToUpper())
                    SortedRefinedNames.Add(Name);

            }

            SortedRefinedNames.Sort();

            foreach (string Name in SortedRefinedNames)
            {
                Console.WriteLine(Name);
            }

            Console.ReadLine();

Thank you very much, it did worked.

Good to hear, mark as solved if nothing further is required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.