I have a C# application with SQL database. I am taking a calendar day of the week, i.e., 1, 5, 10, 11 etc; and am converting it to an integer and inserting into SQL table defined as an 'int'.
However, because Microsoft sorts numbers as text by default, it displays the number "11" before "2" because the first character "1" is less than "2".
When I researched how to get around this, it was suggested to use a Custom Comparer that treats the numbers as integers rather than strings. Please see the following URL to see the CC.
My question is this. How do I implement this new Custom Comparer, so that with each integer day of the week that I want to insert into my SQL database, I call the Comparer routine to have those integers sorted the way I need?
I have attached a section of my code, where I need to know how to call the Comparer.
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL17.SQLEXPRESS\MSSQL\DATA\PayDueDB.mdf;Integrated Security=True;Connection Timeout=30;User Instance=False"))
{
conn.Open();
using (SqlCommand cmdAddPayee = new SqlCommand(
"INSERT INTO PayeeInfo (PayeeName, Description, BillingMonth, DueDate) VALUES (@PayeeName, @Description, @BillingMonth, @DueDate)", conn))
{
cmdAddPayee.Parameters.Add("@PayeeName", SqlDbType.NVarChar).Value = payName;
cmdAddPayee.Parameters.Add("@Description", SqlDbType.NVarChar).Value = theDesc;
cmdAddPayee.Parameters.Add("@BillingMonth", SqlDbType.NVarChar).Value = monthName;
theDay = Convert.ToInt32(arrivalDay);
numbers.Sort(new IntegerComparer());
//foreach (var number in numbers)
//{
// return number(theDay);
//}
cmdAddPayee.Parameters.Add("@DueDate", SqlDbType.Int).Value = theDay;
int response = cmdAddPayee.ExecuteNonQuery();
public class IntegerComparer : IComparer
{
public int Compare(int x, int y)
{
return x.CompareTo(y);
}
//public List numbers = new List { 1, 11, 2, 3, 10 };
}