Back to the Blog
Ok, so I know I’ve neglected this thing for a while now…and I’m due to post several things here from past code camp talks (one of which went extremely well, the other of which was a completely and utter disaster), but the business has completely taken off the last few months and I haven’t had time to devote to this…however, last night at the Softec meeting (www.softec.org) Roberto Monge did a presentation on Web 2.0 that was very intriguing and got me motivated to try and get back to posting things here.
This video on YouTube sums up the presentation Roberto gave very well:
http://www.youtube.com/watch?v=6gmP4nk0EOE
Roberto highlighted a lot of what Web 2.0 is really about: the ability to collaborate on content, to teach and alter how the web works through tagging and ratings of content and so forth, and how Web 2.0 is not really anything more than a living platform for web content…the presentation was great in that it was the first one in a while where the group hung around to talk after the fact thanks to all the good questions and the timeliness of the topic.
So now, I’m motivated 🙂 Let’s see how long it lasts!
Rob
Central Coast Code Camp
Ok so the site is ugly so far since I don’t have time to get a graphic designer to help out…but I’ve got this grand vision of bringing the code camp concept to the California Central Coast so that we don’t have to drive forever to go to conferences or catch up on new technology or pay bunches of bucks…with that in mind:
http://www.centralcoastcodecamp.com
I’ve started preliminary work on finding a facility, and you can see from the Manifesto that there will be a lot of legwork to get enough volunteers and support from local companies to make this happen. But you’ve got to start somewhere and one thing I learned when I started the San Luis Obispo .NET User Group was sometimes you have to draw your line in the sand and just have a meeting to get things started.
This is going to be open to technologies of all types so this will be a good way for me to reach out to my development brethren amongst the heavy Java/PHP developers in the area and try to get us all together.
It’s either going to be great fun or a great disaster! But either way it’s going to be great.
Rob
Sorting Generic Lists
One of the things I like best about the use of Generics is the ability to define your own sorting functionality and apply it to a List. Much in the same way you can predefine your own Find mechanism using predicates, you can establish a way to sort your list of Generics when they don’t contain native datatypes such as int or string.
In this example we’ll take a look at how to sort a Generic list using the IComparer interface.
First of all, let’s establish a class of type Dog, and give it three properties. The first property we’ll call Species, and the next property we’ll call Age. The final property we’ll call Name.
public class Dog
{
private int _age;
private string _species;
private string _name;
public string Name
{
get{ return _name; }
set{ _name = value; }
}
public int Age
{
get{ return _age; }
set{ _age = value; }
}
public string Species
{
get{ return _species; }
set{ _species = value; }
}
}
Now that we have that we can create a list of four dogs like so:
Dog max = new Dog();
max.Age = 8;
max.Species = “Labrador”
max.Name = “Max”
Dog spike = new Dog();
spike.Age = 3;
spike.Species = “Rottweiler”
spike.Name = “Spike”
Dog princess = new Dog();
princess.Age = 5;
princess.Species = “Poodle”
princess.Name = “Princess”
Dog wolfy = new Dog();
wolfy.Age = 10;
wolfy.Species = “Husky”
wolfy.Name = “Wolfy”
List<Dog> dogs = new List<Dog>();
dogs.Add(max);
dogs.Add(spike);
dogs.Add(princess);
dogs.Add(wolfy);
Now I’d like to be able to sort a few different ways, and I can do this by providing an object that implements an IComparer interface of type Dog, like so:
public class DogAgeComparer : IComparer<Dog>
{
}
Now that I have my class, let’s implement the interface:
public class DogAgeComparer : IComparer<Dog>
{
#region IComparer<Dog> Members
public int Compare(Dog x, Dog y)
{
return x.Age.CompareTo(y.Age);
}
#endregion
}
Now that we have that, we can sort our list based on the dog’s age:
dogs.Sort(new DogAgeComparer());
This will sort the list in the appropriate order of age.
Similarly, we can use a different object for Name and have that available as well.
public class DogNameComparer : IComparer<Dog>
{
#region IComparer<Dog> Members
public int Compare(Dog x, Dog y)
{
return x.Name.CompareTo(y.Name);
}
#endregion
}
Now sort the list:
dogs.Sort(new DogNameComparer());
And there you have it! Quick and easy sorting of your objects.
Happy sorting!
Rob
