D
Dylan Parry
Hi,
I have an array of objects of the following class:
public class Appendix
{
private int id;
private int document;
private string title;
private string number;
private string text;
}
The 'id' represents the ID column in the database, the 'document' is the
ID of the parent object, the 'title' is a string representing the title
of the appendix, 'number' is a string (I'll explain that shortly)
representing the appendix number, and finally 'text' contains the text
of the appendix. All pretty self-explanatory, except for the number.
The 'number' property is a string because different documents number
their appendices in different ways. Some label them A-Z and others 1-99,
so the database was set up in such a way that it has a varchar column
for the number. This is mildly annoying, but can't be changed now.
This is all well and good until there are more than 9 appendices in a
document, and they are labelled numerically. When they come out of the
database they are in the order 1, 10, 11, 2, 3, ... etc.
What I want to do is sort the array of objects based on the 'number' but
sort them so that they come out as 1, 2, 3, ... , 10, 11, without
screwing up the sorting when they are ordered as A, B, C, ... etc.
Is there a way to do this? Presumably I'll have to implement IComparable
and write a CompareTo method, but I really have no idea where to begin
with the logic of how to compare strings to get the desired result!
I have an array of objects of the following class:
public class Appendix
{
private int id;
private int document;
private string title;
private string number;
private string text;
}
The 'id' represents the ID column in the database, the 'document' is the
ID of the parent object, the 'title' is a string representing the title
of the appendix, 'number' is a string (I'll explain that shortly)
representing the appendix number, and finally 'text' contains the text
of the appendix. All pretty self-explanatory, except for the number.
The 'number' property is a string because different documents number
their appendices in different ways. Some label them A-Z and others 1-99,
so the database was set up in such a way that it has a varchar column
for the number. This is mildly annoying, but can't be changed now.
This is all well and good until there are more than 9 appendices in a
document, and they are labelled numerically. When they come out of the
database they are in the order 1, 10, 11, 2, 3, ... etc.
What I want to do is sort the array of objects based on the 'number' but
sort them so that they come out as 1, 2, 3, ... , 10, 11, without
screwing up the sorting when they are ordered as A, B, C, ... etc.
Is there a way to do this? Presumably I'll have to implement IComparable
and write a CompareTo method, but I really have no idea where to begin
with the logic of how to compare strings to get the desired result!