Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I need urgent a class in c# windows form that can sort the data from a textfile
For Example I have a file data.txt
AAAAA
BBBBB
CCCCC
AAAAA
BBBBB

and the sort to be like that
AAAAA
AAAAA
BBBBB
BBBBB
CCCCC

Please help me I need it so much.

Thank you
Posted
Updated 14-Apr-12 8:10am
v2
Comments
Nelek 14-Apr-12 13:15pm    
Please, next time you want to answer someone who wrote a solution, use the "Have a question or comment" button below the message of the person you want to answer to. He/she will get a notification and it keeps the thread cleaner. Adding answers just to comment or give feedback to other user makes it a bit chaotical

You don't need a class at all -it's only two lines of code!
C#
string[] lines = File.ReadAllLines(fileName);
Array.Sort(lines);
 
Share this answer
 
Comments
Kuldeep B 14-Apr-12 12:47pm    
Nice Solution Griff......
RaisKazi 14-Apr-12 14:10pm    
My 5.
The text can be read and sorted as given in solution 1 by OriginalGriff. Now, to use OpenFileDialog and to display in ListBox, use the following code in the Click event of the Button
C#
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
string[] dataFromFile = System.IO.File.ReadAllLines(openFileDialog.FileName);
Array.Sort(dataFromFile);
listBox1.DataSource= dataFromFile;
 
Share this answer
 
Comments
h7h7h7 14-Apr-12 12:43pm    
Thankyou very much friends, it works.....
My professor told me to look for a standard class in c#, tha will be the same for all textfile.... but I think like OriginalGriff. in this case we dont need class...

Thank you !
VJ Reddy 14-Apr-12 13:10pm    
You're welcome.
Kuldeep B 14-Apr-12 12:50pm    
VJ Rock Answer For Beginner..
VJ Reddy 14-Apr-12 13:10pm    
Thank you.
RaisKazi 14-Apr-12 14:11pm    
My 5.
Yes I understand that. But the problem is like that.
I want to open file with openfile dialog, then when I click button SORT, to show the data sorted in any listbox....

Please do you have solution for that !
 
Share this answer
 
Or better to explain you, I need to compare all data in a file, and then do sort them...!!!

If anyone can help...?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900