How to sort a listBox not by the first letter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nzsquall
    New Member
    • Oct 2009
    • 22

    How to sort a listBox not by the first letter?

    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
    Code:
      private void sortBySButton_Click(object sender, EventArgs e)
            {
                contactListBox.Items.Clear();
                StreamReader fileReader;
                string[] myArray;
                string line = "";
                string bigString = "";
                fileReader = new StreamReader(@"c: \client contact details.txt");
                while (fileReader.Peek() != -1)
                {
                    line = fileReader.ReadLine();
                    bigString = bigString + line + "\n";
                }
                fileReader.Close();
                bigString = bigString.Trim();
                myArray = bigString.Split('\n');
                Array.Sort(myArray);
                foreach (string item in myArray)
                {
                    contactListBox.Items.Add(item);
                }
            }
    Can someone tell me how to specify it to be sorted by clients' surname?
    Attached Files
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    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

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      • Nzsquall
        New Member
        • Oct 2009
        • 22

        #4
        Originally posted by Banfa
        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.
        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

        Comment

        • PRR
          Recognized Expert Contributor
          • Dec 2007
          • 750

          #5
          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.

          Comment

          • Nzsquall
            New Member
            • Oct 2009
            • 22

            #6
            How to sort a list box?

            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.
            Code:
             private void sortBySButton_Click(object sender, EventArgs e)
                    {
                        contactListBox.Items.Clear();
                        StreamReader fileReader;
                        string[] myArray;
                        string line = "";
                        string bigString = "";
                        fileReader = new StreamReader("client contact details.txt");
                        while (fileReader.Peek() != -1)
                        {
                            line = fileReader.ReadLine();
                            bigString = bigString + line + "\n";
                        }
                        fileReader.Close();
                        bigString = bigString.Trim();
                        myArray = bigString.Split('\n');
                        Array.Sort(myArray);
                        foreach (string item in myArray)
                        {
                            contactListBox.Items.Add(item);
                        }
                    }
            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.

            Code:
            private void sortByLButton_Click(object sender, EventArgs e)
                    {
                        
                    }
            P.S. location is retrieved from locationComboBo x.
            Last edited by tlhintoq; Oct 8 '09, 02:42 PM. Reason: [CODE] ...your code here...[/CODE] tags added

            Comment

            • ssnaik84
              New Member
              • Aug 2009
              • 149

              #7
              my suggestion is..
              - read file into DataTable
              - bind DataTable to ListBox

              and on button click event,
              - sort DataTable and re-bind to ListBox

              Comment

              • Nzsquall
                New Member
                • Oct 2009
                • 22

                #8
                How

                Thanks very much, I appreciate it. But how can I read file into DataTable and bind read file into ListBox?

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  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.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    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?

                    Comment

                    • GaryTexmo
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1501

                      #11
                      Originally posted by Nzsquall
                      Thanks very much, I appreciate it. But how can I read file into DataTable and bind read file into ListBox?
                      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#:


                      Then have a look at this article, which shows examples on how to use DataTables.


                      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 :)

                      Comment

                      • tlhintoq
                        Recognized Expert Specialist
                        • Mar 2008
                        • 3532

                        #12
                        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.

                        Comment

                        • Nzsquall
                          New Member
                          • Oct 2009
                          • 22

                          #13
                          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?

                          Comment

                          Working...