This C# program demonstrates how to sort an array with a delegate.
using System; namespace XoaX { class Program { public class CCharge { public string msName; public decimal mtTotal; } static void Main(string[] args) { // Initialize an array CCharge[] qaCharges = new CCharge[] { new CCharge { msName = "Full Service", mtTotal = 48.0M }, new CCharge { msName = "Washing", mtTotal = 15.0M }, new CCharge { msName = "Maintenance", mtTotal = 20.0M }, new CCharge { msName = "Free Inspection", mtTotal = 0.0M } }; // Sort the array by mtTotal with a delegate Array.Sort(qaCharges, delegate(CCharge qS1, CCharge qS2) { return qS1.mtTotal.CompareTo(qS2.mtTotal); }); // List the elements System.Console.WriteLine("Sorted List:"); foreach (CCharge qCharge in qaCharges) { System.Console.WriteLine(qCharge.msName + " : " + qCharge.mtTotal); } } } }
Sorted List: Free Inspection : 0.0 Washing : 15.0 Maintenance : 20.0 Full Service : 48.0 Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.