473,569 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to sort a listBox not by the first letter?

22 New Member
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, 443 views)
Oct 6 '09 #1
12 5069
whodgson
542 Contributor
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
9,065 Recognized Expert Moderator Expert
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
Nzsquall
22 New Member
@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 locationComboBo x. Where should I put the syntax and what code should I put?

Many thanks
Oct 6 '09 #4
PRR
750 Recognized Expert Contributor
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
Nzsquall
22 New Member
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 Firstmeetingdat e 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 locationComboBo x.
Oct 8 '09 #6
ssnaik84
149 New Member
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
Nzsquall
22 New Member
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
3,525 Recognized Expert Specialist
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
3,525 Recognized Expert Specialist
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

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

Similar topics

3
2486
by: Mike Top | last post by:
I want a listbox to position to a letter provided by the user. Normally, when the user keys a letter, the listbox positions automatically to the first entry starting with that letter in the first column. I want to allow the user to select the column to be searched. For eaxample, assume a list box that looks like this: ...
9
6981
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have it return the associated records to a listbox. Once the listbox has the records, I want to select a record, which will open a form associated with...
1
957
by: Paul | last post by:
I have a dropdownlist and populate it from a stored procedure with strings like names. When the user has it in focus if the hit a letter the selection will go to the first string starting with that same letter (nice feature). I also have a listbox populated with strings but when the user selects a letter with the control in focus, it does...
1
5633
by: Melson | last post by:
Hi Can anyone give suggestions please. I need to create a list (probably using listbox) and fill with names in the listbox. When I enter a name (maybe Adam) in a program (notepad, MS words, other program), the list of names appear. How can I select the name in the listbox with the keyboard if i want to select the eighth name in the list....
0
4418
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for "word", or "eelttr" for "letter". Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that...
4
4959
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be listed first followed by the files.
2
1906
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially display only. I'm trying to sort these lists alphabetically A->Z. I have the Sort property set to True. When I run the app, the items in each...
4
2868
Mohan Krishna
by: Mohan Krishna | last post by:
Hi Everyone, I'm working with a project, in which i used a listbox and textbox as a combination. Hoping anyone would help me, i explianed my problem:- There is a list of companies (sorted or not) in a listbox. While typing in textbox, as per each letter is being typed, the company names in the list MUST BE SELECTED. For eg: In Textbox ...
13
1774
by: ahilar12 | last post by:
hi all, somebody help me out on this i have a listbox where i have the values as starts with and ends with and next to that is a textbox where we give the input whether the fieldname should start with a particular letter or end with a particular letter say for example the field name lastname is to be executed so what we do is select a...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.