473,472 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorting a generic list by letter and number

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
3 2139
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leszek Klich | last post by:
Hello All ! I have a task: QT library: List ListBox. I generating n random numbers from range 0 to 100. I have to sort it by hand... It has to look nicely. It's my workhome from my school....
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
8
by: Matthew Curiale | last post by:
I am creating an app that lists clients of a company for management of different attributes for that company. The first page is a listing of the companies currently in the database. I have my...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
2
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
3
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.