By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,496 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,496 IT Pros & Developers. It's quick & easy.

How to sort a listBox not by the first letter?

P: 22
Hi, I am having trouble sorting a client contact lists by their surname. Every time when I press the "sort by surname button, it always appear alphabetical order
by clients' first name, because I put them before the surname, it is the first String of the every line in the listBox.
My code under the sortBySurname is
Expand|Select|Wrap|Line Numbers
  1.   private void sortBySButton_Click(object sender, EventArgs e)
  2.         {
  3.             contactListBox.Items.Clear();
  4.             StreamReader fileReader;
  5.             string[] myArray;
  6.             string line = "";
  7.             string bigString = "";
  8.             fileReader = new StreamReader(@"c: \client contact details.txt");
  9.             while (fileReader.Peek() != -1)
  10.             {
  11.                 line = fileReader.ReadLine();
  12.                 bigString = bigString + line + "\n";
  13.             }
  14.             fileReader.Close();
  15.             bigString = bigString.Trim();
  16.             myArray = bigString.Split('\n');
  17.             Array.Sort(myArray);
  18.             foreach (string item in myArray)
  19.             {
  20.                 contactListBox.Items.Add(item);
  21.             }
  22.         }
  23.  
Can someone tell me how to specify it to be sorted by clients' surname?
Attached Images
File Type: jpg 无标题.jpg (8.1 KB, 322 views)
Oct 6 '09 #1
Share this Question
Share on Google+
12 Replies


100+
P: 542
Clearly the surname should come before the given names and initials.
In which case as you say your sort would work as intended.
Failing that, code it to sort on the second capital letter
Oct 6 '09 #2

Banfa
Expert Mod 5K+
P: 8,916
There is no function that automatically finds the second word in a string of an array of strings and sorts the array on that word. You will have to sort by hand or re-order your string with the last word at the front (i.e. surname first).

This is a .NET problem and I am moving it to that forum.
Oct 6 '09 #3

P: 22
@Banfa
Thanks very much!! It solves me a big question. But what if I need to sort by the location in each line? the location is a Combo box called locationComboBox. Where should I put the syntax and what code should I put?

Many thanks
Oct 6 '09 #4

Expert 100+
P: 750
PRR
You could put all information in Dataset create a default view and sort it and bind to controls by looping through the view .
DataView.Sort property.
Oct 6 '09 #5

P: 22
Hello everyone. I am a C# learner. I am having troubles doing my first program.
Part of the function is to sort a list box contain client details.
For instance:
One typical line of the contactListBox is:

Surname Firstname Firstmeetingdate Location Phone

I have work out the "sort by surname" button, it is simple to loop through the contactListBox using an array and sort and return, because the surname is the first string in every single line.
Expand|Select|Wrap|Line Numbers
  1.  private void sortBySButton_Click(object sender, EventArgs e)
  2.         {
  3.             contactListBox.Items.Clear();
  4.             StreamReader fileReader;
  5.             string[] myArray;
  6.             string line = "";
  7.             string bigString = "";
  8.             fileReader = new StreamReader("client contact details.txt");
  9.             while (fileReader.Peek() != -1)
  10.             {
  11.                 line = fileReader.ReadLine();
  12.                 bigString = bigString + line + "\n";
  13.             }
  14.             fileReader.Close();
  15.             bigString = bigString.Trim();
  16.             myArray = bigString.Split('\n');
  17.             Array.Sort(myArray);
  18.             foreach (string item in myArray)
  19.             {
  20.                 contactListBox.Items.Add(item);
  21.             }
  22.         }
Now I am having troubles coding the "sort by location" button, because I don't know where to tell the compiler to recognize the location string.
This has already drived me crazy!!!

Can somebody help me a little? None of the resource I found is suitable for a beginner. Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. private void sortByLButton_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.         }
P.S. location is retrieved from locationComboBox.
Oct 8 '09 #6

ssnaik84
100+
P: 149
my suggestion is..
- read file into DataTable
- bind DataTable to ListBox

and on button click event,
- sort DataTable and re-bind to ListBox
Oct 8 '09 #7

P: 22
Thanks very much, I appreciate it. But how can I read file into DataTable and bind read file into ListBox?
Oct 8 '09 #8

tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Oct 8 '09 #9

tlhintoq
Expert 2.5K+
P: 3,525
In your source text file, is there a delimiter to look for? Is there a tab or comma between fields? Where did the source text file come from?
Oct 8 '09 #10

GaryTexmo
Expert 100+
P: 1,501
@Nzsquall
You'll generally need to parse the data in the file and then put it into your DataTable. To do this, you might as well start from the basics... google comes in handy here.

Read this article on how to read and write files in C#:
http://www.devhood.com/Tutorials/tut...utorial_id=400

Then have a look at this article, which shows examples on how to use DataTables.
http://dotnetperls.com/datatable-use

If those aren't exactly what you need, try a little experimentation and/or a few different tutorials.

In my opinion though, don't do any binding... do you need the DataTable? I dunno, I just find it gets a little messy. Read the items and put them into some kind of array or list, then sort that list and populate the ListBox from that. Don't get me wrong, if you're set on binding then by all means go that route, just trying to provide an alternative :)
Oct 8 '09 #11

tlhintoq
Expert 2.5K+
P: 3,525
Nzsquall, in the future please don't start multiple threads in different areas for the same question. It just makes it hard for those trying to help you. It's much better if everyone can see everyone else's suggestion in a cohesive effort.
Oct 8 '09 #12

P: 22
I still don't quite understand.
There might be an easier way that use array to split the character string where it have "\t", then use loops or nested loops to get the value in the location column, and put back into an new string variable, put it into an array and sort the array.
I assume that all the codes are the same till myArray = bigString.Split('\t');
But I am not sure how to get the value from the location column, can somebody help me?
Oct 10 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.