Manipulation On A List C#

need help with sorting and counting in a List.


Have something like this: List<Person> people =new List();
where Person has a Zone and Location attribute.


Jack           1                 2
Larry           2                3
Jill               1                 4
Conner       4                  2
Amy           2                  4
Ben            3                  1
Jeff            1                 1

I want to do 2 things:

1) Count how many people are in each zone
2) Sort the data by zone then location
3) Save all the people in a certain zone to another list
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1 and 3 are easy:
public int CountPeopleInZone(int intZone)
  int intRet =0;
  foreach(Person p in people)
      if(p.Zone==intZone) intRet++;
  return intRet;

public List<People> GetPeopleInZone(int intZone)
  List<People> lstRet = new List<People>();
  foreach(Person p in people)
      if(p.Zone==intZone) lstRet.Add(p);
  return lstRet;

for #2 you can do it a lot of different ways, but none are as simple as the ones above.  A list object has a sort method you can use, but since you're using a "people" object you'd have to implement your own IComparer.

A simpler way without learning anything new (ie implementing your own icomparer) is to just look into some C# sort routines such as bubble sort, and such and manually write a sorting routine.
npl77Author Commented:
What if you dont know how many distinct zones are in the list? How do you get that?
I would prefer using datatables / dataset / dataviews that have builtin sorting / counting functions

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I agree it would be much easier throwing this into a datatable and sorting it using dataview.  You could also then easily get distinct zones in the list that way easily.

check out the following link to see how you can do mini queries on your data table:

Here is a complete example.
  static void Main(string[] args)
    List<Person> people = new List<Person>();
    people.Add(new Person("Jack", 1, 2));
    people.Add(new Person("Larry", 2, 3));
    people.Add(new Person("Jill", 1, 4));
    people.Add(new Person("Conner", 4, 2));
    people.Add(new Person("Amy", 2, 4));
    people.Add(new Person("Ben", 3, 1));
    people.Add(new Person("Jeff", 1, 1));
    //1) Count how many people are in each zone
    Console.WriteLine("ZONE" + " " + "PEOPLE" + "\r\n");
    foreach (KeyValuePair<int, List<Person>> zone in GetZones(people))
      Console.WriteLine(zone.Key.ToString().PadLeft(4) + " " + zone.Value.Count.ToString().PadLeft(6));
    //   1      3
    //   2      2
    //   4      1
    //   3      1
    //2) Sort the data by zone then location
    Console.WriteLine("NAME".PadRight(10) + " " + "ZONE" + " " + "LOCATION" + "\r\n");
    foreach (Person person in people)
      Console.WriteLine(person.Name.PadRight(10) + " " + person.Zone.ToString().PadLeft(4) + " " + person.Location.ToString().PadLeft(8));
    //Jeff          1        1
    //Jack          1        2
    //Jill          1        4
    //Larry         2        3
    //Amy           2        4
    //Ben           3        1
    //Conner        4        2
    //3) Save all the people in a certain zone to another list
    List<Person> zone1People = GetZones(people)[1];
    Console.WriteLine("NAME".PadRight(10) + " " + "LOCATION" + "\r\n");
    foreach (Person zonePerson in zone1People)
      Console.WriteLine(zonePerson.Name.PadRight(10) + " " + zonePerson.Location.ToString().PadLeft(8));
    //NAME       LOCATION
    //Jeff              1
    //Jack              2
    //Jill              4
  static Dictionary<int, List<Person>> GetZones(List<Person> people)
    Dictionary<int, List<Person>> zones = new Dictionary<int, List<Person>>();
    foreach (Person person in people)
      if (zones.ContainsKey(person.Zone))
        zones.Add(person.Zone, new List<Person>(new Person[] { person }));
    return zones;
  class Person : IComparable<Person>
    public string Name;
    public int Zone;
    public int Location;
    public Person(string name, int zone, int location)
      this.Name = name;
      this.Zone = zone;
      this.Location = location;
    public int CompareTo(Person other)
      if (Zone == other.Zone)
        if (Location == other.Location)
          return Name.CompareTo(other.Name);
          return Location.CompareTo(other.Location);
        return Zone.CompareTo(other.Zone);

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.