Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class Bill
  2. {
  3. private string billNumber;
  4. private string billDate;
  5. private DateTime date;
  6. private string from;
  7. private string to;
  8. private string billCost;
  9. private string power1;
  10. private string power2;
  11. private string power3;
  12. private string power4;
  13. private string power5;
  14. private string power6;
  15. private string contractNumber;
  16. }
  17.  
  18. class Contract
  19. {
  20. private List<Bill> billList;
  21. private Dictionary<double, Bill> billsDictionary;
  22. private double contractNumber;
  23. }
  24.  
  25. class Pet
  26. {
  27. public string Name { get; set; }
  28. public int Age { get; set; }
  29. }
  30.  
  31. public static void OrderByEx1()
  32. {
  33. Pet[] pets = { new Pet { Name="Barley", Age=8 },
  34. new Pet { Name="Boots", Age=4 },
  35. new Pet { Name="Whiskers", Age=1 } };
  36.  
  37. IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);
  38.  
  39. foreach (Pet pet in query)
  40. {
  41. Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
  42. }
  43. }
  44.  
  45. /*
  46. This code produces the following output:
  47.  
  48. Whiskers - 1
  49. Boots - 4
  50. Barley - 8
  51. */
  52.  
  53. public class Comp : IComparer<Bill>
  54. {
  55. public int Compare(Bill x, Bill y)
  56. {
  57. // remember to handle null values first (x or y or both are nulls)
  58. return x.date.CompareTo(y.date);
  59. }
  60. }
  61.  
  62. billList.Sort(new Comp());
  63.  
  64. billList = billList.OrderBy(bill => bill.date).ToList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement