close

image

1 public class iArray : IComparable 2 { 3 private int x, y; 4 public iArray() 5 { 6 x = 0; y = 0; 7 } 8 public iArray(int ix, int iy) 9 { 10 x = ix; 11 y = iy; 12 } 13 public void Show() 14 { 15 Console.WriteLine("({0}.{1})", x, y); 16 } 17 int IComparable.CompareTo(object obj) 18 { 19 iArray v = (iArray)obj; 20 return (x * x + y * y) - (v.x * v.x + v.y * v.y); 21 } 22 }


測試

1 class Program 2 { 3 static void Main(string[] args) 4 { 5 iArray[] vec = { new iArray(20, 10), 6 new iArray(50, 20), 7 new iArray(90, 40), 8 new iArray(10, 10), 9 new iArray(40, 30)}; 10 Console.WriteLine("排序前..."); 11 foreach (iArray v in vec) 12 { 13 v.Show(); 14 } 15 16 Console.WriteLine("排序後(ascending)..."); 17 Array.Sort(vec); 18 foreach (iArray v in vec) 19 { 20 v.Show(); 21 } 22 Console.WriteLine("排序後(descending)..."); 23 Array.Reverse(vec); 24 foreach (iArray v in vec) 25 { 26 v.Show(); 27 } 28 29 Console.Read(); 30 } 31 }


image



1 public class iArray : IComparable 2 { 3 private int x, y; 4 public iArray() 5 { 6 x = 0; y = 0; 7 } 8 public iArray(int ix, int iy) 9 { 10 x = ix; 11 y = iy; 12 } 13 public void Show() 14 { 15 Console.WriteLine("({0}.{1})", x, y); 16 } 17 int IComparable.CompareTo(object obj) 18 { 19 iArray v = (iArray)obj; 20 return (x * x + y * y) - (v.x * v.x + v.y * v.y); 21 } 22 }
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()