Arrange ListBox According To Company Name

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am attempting to arrange a listbox according to company name....I have the listbox loaded with peoples names and the company they work for like so

example
Joe - UPS
Tim - UPS
Sara - FPL
Scott - Coastal
Alex - UPS
Luis - FPL

there will be an unknown number of names in the listbox......what I need to do is arrange the listbox so that all the company names are itogether and the names that have only one company, they need to be at the end....the code I wrote kind of works...but not quite....please take a look at it and help me figure out how to do this.

 private List<string> Arrange_List()
        {
            if (lstPlayers.Items.Count < 1)
            {
                return matchedCompany_List;
            }
            else
            {
                for (int i = 0; i < lstPlayers.Items.Count; i++)
                {

                    for (int y = i + 1; y <= i + 1; y++)
                    {
                        if (i == lstPlayers.Items.Count - 1)
                        {
                        }
                        else
                        {
                            string[] first_values = lstPlayers.Items[i].ToString().Split('-');
                            string first_company = first_values[1];
                            string[] second_values = lstPlayers.Items[y].ToString().Split('-');
                            string second_company = second_values[1];
                            if (second_company == first_company)
                            {
                                matchedCompany_List.Add(lstPlayers.Items[i].ToString());
                                matchedCompany_List.Add(lstPlayers.Items[y].ToString());
                            }
                            else
                            {
                                unmatchedCompany_List.Add(lstPlayers.Items[y].ToString());
                            }
                        }
                    }
                }
            }
                return matchedCompany_List;
          
                
        }



Thank You for any help

-InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Actually what I mean is ..there will be multiple companies..some could have 15 names , some might have only one name...by names I mean people names....I am creating a football pool program...there are multiple companies in this pool and in total there are about 50 or so people in this football pool...what I am creating is a program to keep track of the "players" in the football pool...and arrange the player names by the company where they work. I would like to have all the same company names together in the listbox..

example :

this is the listbox -
Joe - UPS
Tim - UPS
Sara - FPL
Scott - Coastal
Alex - UPS
Luis - FPL

and after the arrange method the listbox should look like this

example :
Joe - UPS
Tim - UPS
Alex - UPS
Sara - FPL
Luis - FPL
Scott - Coastal

Thank You for the help
-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
I can tell you that it's nothing to do with the ListBox. You will have to arrange the data in the order you want and then populate the ListBox.

What comes to mind immediately is to separate each item into person and company, then use LINQ to group by company, then order by person count and then you can simply loop through the groups to get all the companies and within each group to get all the people.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Yes...I thought about doing it that way...was hoping there was a easier solution....I have a Player class that has the player name , company and all the football picks for that week. As each player is added the xml is written with the player details..then the player names are read from the xml and loaded into the listbox. thats why the list box is not arranged.

so what you are saying is to create a List<Player> and sort the list before the xml is written?

Thank You for your help
-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
so what you are saying is to create a List<Player> and sort the list before the xml is written?
Basically, yes, although the sorting can be done before writing or after reading.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Actually...sorting after reading would be easier, I think.....

Thank You for the Help

-InkedGFX
 
Top Bottom