By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

Sorting a generic list by letter and number

P: n/a
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.
Jun 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ASP Developer wrote:
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
Jun 12 '06 #2

P: n/a
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" wrote:
ASP Developer wrote:
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

Jun 12 '06 #3

P: n/a
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

ASP Developer wrote:
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.

Jun 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.