Click here to Skip to main content
15,921,169 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I want to sort data,
I call the file from a path in my PC
Ex.
C#
String[] data=file.readalllines(textbox.text);


the file has those data.
Cristiano Ronaldo
LIonel Messii
-------------------
Karim Benzema
Rikardo Kak
------------------
Van Persi
mario gomes
------------------
Now, when I click button the data I want to be sorted without lines "--------" or spaces " " ......
I am using
C#
data=data.replace("---------" , " " );
but it doesn't work...
Posted
Updated 19-Apr-12 22:36pm
v2
Comments
h7h7h7 20-Apr-12 4:43am    
p.s

So when I click the button the sorted should be"
Cristiano Ronaldo
Karim Benzema
Lionel Messi
Mario Gomes
Rikardo Kaka
Van Persi

I am using array.sort(data) ... but it doesnt work if I want to remove "------" as I mention in first comment

I am using that, and there are again no error....

string [] lst = listBox1.Items.Cast<string>().ToArray();

var sortedList = lst.OrderBy(s => s).
Where(s => !(s.Contains("----") || s.Contains(" ")));


listBox2.DataSource = sortedList.ToList();


but when I click button thera are no data sorted ??
 
Share this answer
 
My solution is like that

C#
string[] dataFromFile = listBox2.Items.Cast<string>().ToArray();

            Array.Sort(dataFromFile);
            listBox1.DataSource = dataFromFile;


but I don't know to implement code to remove that string "---------".

andy gave me a good solution, but in this code I can't implement
 
Share this answer
 
v2
You can try using LINQ/Lambda Expressions like this:

C#
string [] player = { "Zidane","Gomez", "Messi", "----", "Ballack", " " };

var sortedList = player.OrderBy(s => s).
                        Where(s => !(s.Contains("----") || s.Contains(" ")));


Will result in
Ballack
Gomez
Messi
Zidane



BTW It's Mario Gomez not Gomes ;-)

If the strings ar Items in a ListBox, then try

C#
listBox.Sorted = true;
listBox.Items.Remove("----");
listBox.Items.Remove(" ");


OK, last try:

I assume you have a WinForm app. I dont' know, why my listBox.Sorted = true; solution did not work. I wrote a little test app and it does well.

C#
string [] lst = listBox1.Items.Cast<string>().ToArray();

var sortedList = lst.OrderBy(s => s).
                     Where(s => !(s.Contains("----") || s.Contains(" ")));


listBox2.DataSource = sortedList.ToList();
 
Share this answer
 
v3
Comments
h7h7h7 20-Apr-12 6:22am    
haha yes it is Gomoz..
Andy, my data are in listbox, I don need to create new list...
I tried
var sortedlist=listbox.item.OrderBy(s => s).
Where(s => !(s.Contains("----") || s.Contains(" ")));

but listbox.item.( here orderby is unkonwn, what should I do )..
Andy411 20-Apr-12 7:10am    
Try:
listBox.Sorted = true;
listBox.Items.Remove("----");
listBox.Items.Remove(" ");
h7h7h7 20-Apr-12 7:23am    
It's the same again.... :(
Andy411 20-Apr-12 8:03am    
I made a little WinForm app and it works fine...
VJ Reddy 20-Apr-12 11:14am    
Good answer. +5
You can use Trim Function[^] to remove such spaces.

Check this one also: Trim Examples[^]
 
Share this answer
 
Comments
h7h7h7 20-Apr-12 5:44am    
Yes, I know about trim function, but string[] data.... (array) doesn't support
trim, replace, or remove....!??
is there other opportunity ?

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