We help IT Professionals succeed at work.

Manipulation On A List C#

npl77
npl77 asked
on
need help with sorting and counting in a List.

Example:

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

Data:
NAME      ZONE      LOCATION

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
Comment
Watch Question

Commented:
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.

Author

Commented:
What if you dont know how many distinct zones are in the list? How do you get that?
CERTIFIED EXPERT
Commented:
I would prefer using datatables / dataset / dataviews that have builtin sorting / counting functions
Commented:
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:
http://www.akadia.com/services/dotnet_filter_sort.html

Top Expert 2006

Commented:
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));
 
    //ZONE PEOPLE
    //
    //   1      3
    //   2      2
    //   4      1
    //   3      1
 
    Console.WriteLine();
 
    //2) Sort the data by zone then location
 
    people.Sort();
 
    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));
 
    //NAME       ZONE LOCATION
    //
    //Jeff          1        1
    //Jack          1        2
    //Jill          1        4
    //Larry         2        3
    //Amy           2        4
    //Ben           3        1
    //Conner        4        2
 
    Console.WriteLine();
 
    //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
 
    Console.ReadLine();
  }
 
  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[person.Zone].Add(person);
      else
        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);
        else
          return Location.CompareTo(other.Location);
      else
        return Zone.CompareTo(other.Zone);
    }
  }

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.