ºñ±³ÇÔ¼ö¸¦ ¸¸µé¾î list¸¦ sort ÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸ÀÚ.1. Comparison<T>¸¦ ÀÌ¿ë¿øÇü : public delegate int Comparison<T>(T x, T y)
µ¨¸®°ÔÀÌÆ® ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ´Ù. public class SortTest
{ public int ByComparison(float x, float y) { return x.CompareTo(y); } public void Run() { List<float> list = new List<float>(){ 30, 20, -10}; list.Sort(ByComparison); Console.WriteLine("{0}, {1}, {2}", list[0], list[1], list[2]); } } °á°ú: -10, 20, 30 2. IComparer<T>¸¦ ÀÌ¿ëÀÎÅÍÆäÀ̽º IComparer¸¦ »ó¼ÓÇÑ Å¬·¡½º¸¦ ÀÌ¿ëÇÑ´Ù.using System;
using System.Collections.Generic; public class Comparer : IComparer<float> { public int Compare(float x, float y) { return x.CompareTo(y); } } List<float> list = new List<float>(){ 30, 20, -10}; list.Sort(new Comparer()); Console.WriteLine("{0}, {1}, {2}", list[0], list[1], list[2]); °á°ú: -10, 20, 30 ¾Æ·¡¿Í °°ÀÌ ¸®ÅϽà ¸¶À̳ʽº¸¦ ÁÖ¸é ¿ª¼øÀ¸·Î Á¤·ÄµÈ´Ù. using System;
using System.Collections.Generic; public class Comparer : IComparer<float> { public int Compare(float x, float y) { return -x.CompareTo(y); //¸¶À̳ʽºÀÌ¸é ¿ª¼øÀ¸·Î Á¤·Ä //°á°ú: 30, 20, -10 } } 3. IComparer<T>·Î ¹üÀ§ ÁöÁ¤¸®½ºÆ®ÀÇ Æ¯Á¤ ¹üÀ§¸¦ ÁöÁ¤ÇÏ¿© Á¤·ÄÇÒ¼ö ÀÖ´Ù.using System;
using System.Collections.Generic; public class Comparer : IComparer<float> { public int Compare(float x, float y) { return x.CompareTo(y); } } List<float> list = new List<float>(){ 30, 20, -10}; list.Sort(1, 2, new Comparer()); //1 À妽º¿¡¼ 2°³ÀÇ ¿ä¼Ò¸¸ Á¤·Ä Console.WriteLine("{0}, {1}, {2}", list[0], list[1], list[2]); °á°ú: 30, -10, 20 1¹ø À妽º¿¡¼ 2°³¸¸ Á¤·ÄÇϱ⠶§¹®¿¡ 0¹ø À妽º´Â Á¤·ÄµÇÁö ¾Ê´Â´Ù. list.Sort(1, 2, new Comparer()); 4. ¶÷´Ù ÀÌ¿ëusing System;
using System.Collections.Generic; public class Comparer : IComparer<float> { public int Compare(float x, float y) { return x.CompareTo(y); } } List<float> list = new List<float>(){ 30, 20, -10}; list.Sort((float x, float y) => { return x.CompareTo(y); }); °á°ú: 30, -10, 20 ÂüÁ¶) http://bravochoi.tistory.com/134 http://pullthelever.tistory.com/479 https://www.dotnetperls.com/icomparable |