sort array of strings using for loop

  • Thread starter intruder83-help
  • Start date
I

intruder83-help

Hi everybody.
I have a problem with sorting an array of names. I have a txt file with a
names e.g. "Jack, John, Bob, Adam, Paul".
All i have to do is write the names into the individual cells of new array
and than sort it by an alphabet using for loop and write it out into a new
txt file.
I just after figuring out how to separete a names and my source code looks
like:

string delimiter = ",";
string path = @"c:\notsorted.txt";
string r = sr.ReadLine();
string[] names = r.Split(delimiter.ToCharArray());

but i have no idea how to sort it with for loop.
If there is anyone who can help me it would be great!
Thanks
 
A

Alberto Poblacion

intruder83-help said:
but i have no idea how to sort it with for loop.
If there is anyone who can help me it would be great!

First of all you have to choose a sorting algorithm. There are plenty of
them; for instance, you can find the description for Bubble Sort in this
page: http://en.wikipedia.org/wiki/Bubblesort . The wikipedia article
contains some sample code in various programming languages. Follow the links
at the bottom of the page for other sorting algorithms.
 
F

Family Tree Mike

Hi everybody.
I have a problem with sorting an array of names. I have a txt file with a
names e.g. "Jack, John, Bob, Adam, Paul".
All i have to do is write the names into the individual cells of new array
and than sort it by an alphabet using for loop and write it out into a new
txt file.
I just after figuring out how to separete a names and my source code looks
like:

string delimiter = ",";
string path = @"c:\notsorted.txt";
string r = sr.ReadLine();
string[] names = r.Split(delimiter.ToCharArray());

but i have no idea how to sort it with for loop.
If there is anyone who can help me it would be great!
Thanks

Why not use a list instead?

List<string> names = r.Split(delimiter.ToCharArray()).ToList();
names.Sort();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top