[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# - Best practise sorting data

Posted on 2007-10-11
14
Medium Priority
?
227 Views
Last Modified: 2013-11-27
Hi,

public class Company
 {
        public int CompanyId;
        public string CompanyName
        public string Slogan
}

Lets say I have a collection of Companies , sorted by CompanyName in alphabetical order.

List<Company> companies = new List<Company>();

CompanyId    CompanyName         Slogan
10                       Coca Cola           Drink coke
10                       Coca Cola           Coke is good
20                       General Motors   Drive cars
20                       General Motors   Drive more cars
20                       General Motors   Even more cars
30                       Philips                  Use your TVs
..
...
------------------------------------------------------------------

I know a lot of "dirty" code tricks that would do the job, but I think they are too unstable.

In your opinion, what would be the cleanest way to get this endresults:

------------------------------------------------------------------
...loop through the data and do some magic...

The arrayList [0] would only have:
10                        Coca Cola           Drink coke
10                        Coca Cola           Coke is good

The arrayList [1] would only have:
20                       General Motors   Drive cars
20                       General Motors   Drive more cars
20                       General Motors   Even more cars

etc.

Thank you!!
0
Comment
Question by:Endelm
  • 8
  • 6
14 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 20055040
List<Person> people = new List<Person>();

            people.Add(new Person(50, "Fred"));
            people.Add(new Person(30, "John"));
            people.Add(new Person(26, "Andrew"));
            people.Add(new Person(24, "Xavier"));
            people.Add(new Person(5, "Mark"));
            people.Add(new Person(6, "Cameron"));

            people.Sort(new Comparison<Person>(Compare));

            for (int i = 0; i < people.Count; i++)
            {
                Console.WriteLine(people[i].name);
            }

int Compare(Person p, Person pp)
        {
            if (p.name.CompareTo(p.name) > 0)
            {
                return p.age.CompareTo(p.age);
            }
            else
            {
                return p.name.CompareTo(p.name);
            }
        }

public class Person
    {
        public int age;
        public string name;

        public Person(int age, string name)
        {
            this.age = age;
            this.name = name;
        }
    }
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055051
once sorted say to get all the data with age = 10

List<Person> Filterd =
                people.FindAll(new Predicate<Person>(Filter));

bool Filter(Person p)
        {
            return p.age == 10;
        }
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055068
or

List<Person> Filterds =
                people.FindAll(delegate(Person pp) { return pp.age ==10; });        }
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:Endelm
ID: 20055083
It gives me an error in this line:

"people.Sort(new Comparison<Person>(Compare));"

Error      1      The name 'Compare' does not exist in the current context

My Person class looks like this:


    public class Person
    {
        public int age;
        public string name;

        public Person(int age, string name)
        {
            this.age = age;
            this.name = name;
        }

        public int Compare(Person p, Person pp)
        {
            if (p.name.CompareTo(p.name) > 0)
            {
                return p.age.CompareTo(p.age);
            }
            else
            {
                return p.name.CompareTo(p.name);
            }
        }
    }
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055092
did u decalre this function
int Compare(Person p, Person pp)
        {
            if (p.name.CompareTo(p.name) > 0)
            {
                return p.age.CompareTo(p.age);
            }
            else
            {
                return p.name.CompareTo(p.name);
            }
        }
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055098
compare should be static and u shud call AS Person.Compare
0
 

Author Comment

by:Endelm
ID: 20055105
Oh, it works when I put Compare as as static member function inside Person.

Or if I make the function outside the Person class.
0
 

Author Comment

by:Endelm
ID: 20055153
One last question, if I may.

If the data is like this

            people.Add(new Person(10, "Fred"));
            people.Add(new Person(10, "John"));
            people.Add(new Person(20, "Andrew"));
            people.Add(new Person(20, "Xavier"));
            people.Add(new Person(30, "Mark"));
            people.Add(new Person(30, "Cameron"));

How would you sort the names by age like this:

people[0]:
 List<Person> sortedPeople = new List<Person>();
            sortedPeople.Add(new Person(10, "Fred"));
            sortedPeople.Add(new Person(10, "John"));

people[1]
 List<Person> sortedPeople = new List<Person>();
            sortedPeople.Add(new Person(20, "Andrew"));
            sortedPeople.Add(new Person(20, "Xavier"));

...

Thanks a lot! :o)
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055174
first sort

people.Sort(new Comparison<Person>(Compare));


then

List<Person> AgeGroup10 =
                people.FindAll(delegate(Person pp) { return pp.age ==10; });        }



List<Person> AgeGroup20 =
                people.FindAll(delegate(Person pp) { return pp.age ==20; });        }
0
 

Author Comment

by:Endelm
ID: 20055206

Thank you.

The only trouble is that I don´t know the year number beforehand.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055210
that u can have in a variable and the use that variable instead of that number...
0
 

Author Comment

by:Endelm
ID: 20055231
Great! I´ll give it a try.

Thanks for your patience.

The only thing that puzzles me now is this. It seems not to be sorting right.



        public static int Compare(Person p, Person pp)
        {
            if (p.name.CompareTo(p.name) > 0)
            {
                return p.age.CompareTo(p.age);
            }
            else
            {
                return p.name.CompareTo(p.name);
            }
        }
0
 

Author Comment

by:Endelm
ID: 20055246
I found out how:

        public static int Compare(Person p, Person pp)
        {
            return p.name.CompareTo(pp.name);
        }

Thank you.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 20055271
:-)
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question