Warm‑up: Implement IComparable<T> for Support Tickets (with reference answer)
Warm‑up (with answer): You will implement IComparable<T> for a custom type and verify sorting behavior. This warm-up covers: - CompareTo contract: return -1 / 0 / +1 - Multi-field comparison (Primary: Priority, Secondary: CreatedAt, Tertiary: TicketId) - Null handling - Ascending order vs custom descending sort using Comparer - Using List<T>.Sort() (default uses IComparable<T>) - Using OrderBy(...) and ThenBy(...) ✅ The warm-up includes a reference answer (hidden behind a button).
using System; // Console, DateTime
using System.Collections.Generic; // List, Comparer
using System.Globalization; // parsing
using System.Linq; // LINQ
namespace ItTechGenie.M1.IComparable.WarmUp
{
// Ticket model that can be sorted (default) using IComparable<Ticket>
public class SupportTicket : IComparable<SupportTicket> // IComparable<T> enables Sort()
{
public string TicketId { get; } // unique id (may contain spaces/unicode)
public int Priority { get; } // lower number => higher priority
public DateTime CreatedAt { get; } // created time
public string Title { get; } // message/title
public SupportTicket(string ticketId, int priority, DateTime createdAt, string title) // constructor
{
TicketId = ticketId; // assign id
Priority = priority; // assign priority
CreatedAt = createdAt; // assign time
Title = title; // assign title
}
// ✅ Warm-up TODO: Student can compare with the reference answer (below)
public int CompareTo(SupportTicket? other) // contract: -1/0/+1
{
// TODO:
// 1) null other => current is "greater" or "after" (return 1)
// 2) Primary: Priority ascending (1 before 2)
// 3) Secondary: CreatedAt ascending (earlier first)
// 4) Tertiary: TicketId ordinal (string compare)
throw new NotImplementedException();
}
public override string ToString() // print
=> $"{Priority} | {CreatedAt:HH:mm:ss} | {TicketId} | {Title}";
}
internal class Program
{
static void Main()
{
// sample tickets (hard-coded for warm-up)
var tickets = new List<SupportTicket> // list of comparable tickets
{
new SupportTicket("TKT- 001 ✅", 2, DateTime.Parse("2026-02-18 10:05:02"), "Login fail!@#"),
new SupportTicket("TKT-α12", 1, DateTime.Parse("2026-02-18 10:04:59"), "Payment ₹ 1,999.25"),
new SupportTicket("TKT-β77", 2, DateTime.Parse("2026-02-18 10:05:02"), "Timeout α/β"),
new SupportTicket("TKT- 001 ✅", 2, DateTime.Parse("2026-02-18 10:05:02"), "Duplicate id")
};
// 1) Default sort uses IComparable<T> (CompareTo)
tickets.Sort(); // relies on CompareTo
Console.WriteLine("Default Sort (Priority, CreatedAt, TicketId):");
tickets.ForEach(t => Console.WriteLine(t)); // print sorted list
// 2) Descending sort example using Comparer (not IComparable)
var desc = tickets.OrderByDescending(t => t.Priority) // higher priority number first
.ThenBy(t => t.CreatedAt) // then by time
.ToList();
Console.WriteLine("\nCustom Sort (Priority DESC, CreatedAt ASC):");
desc.ForEach(t => Console.WriteLine(t));
}
}
}
Warm‑up Reference Answer (Conceptual)
Reference CompareTo implementation (key points): - If other is null: return 1 (current comes after null). - Compare Priority first (ascending): Priority.CompareTo(other.Priority). - If equal: compare CreatedAt (ascending): CreatedAt.CompareTo(other.CreatedAt). - If equal: compare TicketId (ordinal): string.Compare(TicketId, other.TicketId, StringComparison.Ordinal). - Return first non-zero result; otherwise return 0. This ensures deterministic sorting when multiple fields are equal.