Sajmon

Example of IComparable

May 9th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. /**
  7. @Author: Sajmon
  8. */
  9. namespace practise3
  10. {
  11.     public class Employee : IComparable
  12.     {
  13.         private String name;
  14.         private int salary;
  15.  
  16.  
  17.         public Employee(String name, int sal) {
  18.             this.name = name;
  19.             this.salary = sal;
  20.         }
  21.  
  22.  
  23.         #region Properties
  24.         public String Name {
  25.             get
  26.             {
  27.                 return this.name;
  28.             }
  29.             set
  30.             {
  31.                 this.name = value;
  32.             }
  33.         }
  34.  
  35.         public int Salary {
  36.             get {
  37.                 return this.salary;
  38.             }
  39.             set {
  40.                 this.salary = value;
  41.             }
  42.  
  43.         }
  44.         #endregion
  45.  
  46.         public int CompareTo(object obj)
  47.         {
  48.             Employee other = (Employee) obj;
  49.             if (this.salary < other.salary) {
  50.                 return 1;
  51.             }
  52.             else if (this.salary > other.salary)
  53.             {
  54.                 return -1;
  55.             }
  56.             else {
  57.                 return 0;
  58.             }
  59.  
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. using System;
  67. using System.Collections.Generic;
  68. using System.Linq;
  69. using System.Text;
  70. using practise3;
  71.  
  72. /**
  73. @Author: Sajmon
  74. */
  75. namespace Program
  76. {
  77.     class Program
  78.     {
  79.         static void Main(string[] args)
  80.         {
  81.             List<Employee> employees = new List<Employee>();
  82.             employees.Add(new Employee("Peter", 3000));
  83.             employees.Add(new Employee("Jano", 12500));
  84.             employees.Add(new Employee("Fero", 17500));
  85.             employees.Add(new Employee("Martin", 22500));
  86.             employees.Add(new Employee("Andrej", 32500));
  87.             employees.Add(new Employee("Jozko", 500));
  88.             employees.Add(new Employee("Rado", 15500));
  89.             employees.Add(new Employee("Miro", 18500));
  90.             employees.Add(new Employee("Ondrej", 11500));
  91.             employees.Sort();
  92.  
  93.             foreach (var item in employees) {
  94.                 Console.WriteLine(item.Name + ", " + item.Salary);
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment