Sorting a generic list by letter and number

G

Guest

I have a generic list of objects. These objects have a text field that
contains a letter for the first character and numbers for the next three.
For example, "A001".
I need to sort these by letter first and then by number. What is the best
way to sort these?

Any help would be greatly appreciated.
 
D

Dan Manges

ASP said:
I have a generic list of objects. These objects have a text field that
contains a letter for the first character and numbers for the next three.
For example, "A001".
I need to sort these by letter first and then by number. What is the best
way to sort these?

Any help would be greatly appreciated.

You can either use a Comparison delegate or have your object implement
IComparer.

Here's an example using a comparison:

class Program
{
static void Main(string[] args)
{
List<MyObject> list = new List<MyObject>();
list.Add(new MyObject("Z001"));
list.Add(new MyObject("A100"));
list.Add(new MyObject("C234"));
list.Add(new MyObject("B224"));
list.Sort(new Comparison<MyObject>(compareMethod));
foreach (MyObject myObject in list)
Console.WriteLine(myObject.TextField);
Console.ReadLine();
}

static int compareMethod(MyObject one, MyObject two)
{
return String.Compare(one.TextField, two.TextField);
}

private class MyObject
{
private string textField;

public MyObject(string textField)
{
this.textField = textField;
}

public string TextField
{
get
{
return this.textField;
}
}
}
}

Output:

A100
B224
C234
Z001


Hope this helps.

Dan Manges
 
G

Guest

Dan,

Thanks for the reply. I need to fulfill one more thing with this type of
sorting. I need to be able to specify which order the letters sort in. For
example,

Z300
B100
R200

Basically the letters are sorted based on some other logic and not
alphabetically. Talk to you soon.

Dan Manges said:
ASP said:
I have a generic list of objects. These objects have a text field that
contains a letter for the first character and numbers for the next three.
For example, "A001".
I need to sort these by letter first and then by number. What is the best
way to sort these?

Any help would be greatly appreciated.

You can either use a Comparison delegate or have your object implement
IComparer.

Here's an example using a comparison:

class Program
{
static void Main(string[] args)
{
List<MyObject> list = new List<MyObject>();
list.Add(new MyObject("Z001"));
list.Add(new MyObject("A100"));
list.Add(new MyObject("C234"));
list.Add(new MyObject("B224"));
list.Sort(new Comparison<MyObject>(compareMethod));
foreach (MyObject myObject in list)
Console.WriteLine(myObject.TextField);
Console.ReadLine();
}

static int compareMethod(MyObject one, MyObject two)
{
return String.Compare(one.TextField, two.TextField);
}

private class MyObject
{
private string textField;

public MyObject(string textField)
{
this.textField = textField;
}

public string TextField
{
get
{
return this.textField;
}
}
}
}

Output:

A100
B224
C234
Z001


Hope this helps.

Dan Manges
 
D

Dan Manges

If you're using a comparison delegate, here is how I would do it for
your data format:

Inside the class, make a char[] of the order.

Example: char[] order = new char[] { 'Z', 'B', 'R' };

In the comparison method:

int compareMethod(MyObject one, MyObject two)
{
// if they start with the same letter, just compare them
if (one.TextField[0] == two.TextField[0])
return String.Compare(one.TextField, two.TextField);
/* loop through the order, first object to start with a letter in
order gets moved up in the order */
foreach (char c in order)
{
if (one.TextField[0] == c)
return -1;
if (two.TextField[0] == c)
return 1;
}
//code shouldn't get here if all possible first letters are
provided in the order array
return String.Compare(one.TextField, two.TextField);
}

This should let you define the custom order you need.

Dan Manges
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top