티스토리 뷰
한글페이지고 뭐고 왜 그리들 어렵게 써놨는지.. C# 초보자는 알수도 없게 해놨던데
외국인이 나같은 초보자도 볼 수 있게 설명해놔서 가지고 왔다.
At the suggestion of my pal Russ Nemhauser, I recently found myself investigating predicates in relation to the Array and List classes in the .NET Framework 2.0. Along the way, I dug a little deeper into sorting arrays, and found a new feature which can save you a bunch of coding.
Imagine that you've got an array of objects, and you want to sort if by one of the properties of the objects. For example, imagine that you've written the following code:
[VB]
Dim di As New DirectoryInfo("C:\")
Dim files() As FileInfo = di.GetFiles("*.*")
[C#]
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] files = di.GetFiles("*.*");
Calling the Array.Sort method for this array gets you an InvalidOperationException, because the Array class has no idea on which property you want it to sort. In previous versions of the .NET Framework, you could solve the problem by creating a class that implements System.Collections.IComparer. In this class, the Compare procedure handles the logic that determines how to sort the information, like this code, which sorts the data based on the FullName property:
[VB]
Public Class CompareNames
Implements IComparer
Public Function Compare( _
ByVal x As Object, ByVal y As Object) _
As Integer _
Implements System.Collections.IComparer.Compare
Dim file1 As FileInfo = CType(x, FileInfo)
Dim file2 As FileInfo = CType(y, FileInfo)
Return file1.FullName.CompareTo(file2.FullName)
End Function
End Class
[C#]
public class CompareNames :
System.Collections.IComparer
{
public int Compare(object x, object y)
{
FileInfo file1 = (FileInfo)x;
FileInfo file2 = (FileInfo)y;
return file1.FullName.CompareTo(file2.FullName);
}
}
To perform the sort, you can write code like this:
[VB]
Dim comparer1 As New CompareNames()
Array.Sort(files, comparer1)
[C#]
CompareNames comparer1 = new CompareNames();
Array.Sort(files, comparer1);
If you then wanted to sort by CreationTime, for example, you would need to create another class, providing a different comparer. What a pain! Not only must you create a new class for each sort order, you must perform the same conversions each time, because the IComparer.Compare method passes Object references, not strongly typed values.
In the .NET Framework 2.0, you have a number of options using generics, but the simplest is to take advantage of the Array.Sort overload that's defined like this:
[VB]
Public Shared Sub Sort(Of T)(array As T(), comparison As Comparison(Of T))
[C#]
Public static void Sort<T>(T[] array, Comparison<T> comparison)
In this overload, you supply a generic array of a particular type, along with a procedure of the appropriate delegate type. The delegate type looks like this:
[VB]
Public Delegate Function Comparison(Of T) (x As T, y As T) As Integer
[C#]
public delegate int Comparison<T> (T x, T y)
All you need to do is create a procedure, like this one, that compares two objects of the same type:
[VB]
Private Function CompareFileNames( _
ByVal f1 As FileInfo, ByVal f2 As FileInfo) As Integer
Return f1.FullName.CompareTo(f2.FullName)
End Function
[C#]
public int CompareFileNames(FileInfo f1, FileInfo f2)
{
return f1.FullName.CompareTo(f2.FullName);
}
It's a lot simpler than creating a new class for each sort, and performing conversions for two objects each time. Given the CompareFileNames procedure, you could perform the sort using code like this:
[VB]
Array.Sort(files, AddressOf CompareFileNames)
[C#]
Array.Sort(files, CompareFileNames);
If you then wanted to sort by CreationTime, you could supply this procedure:
[VB]
Private Function CompareDates( _
ByVal f1 As FileInfo, ByVal f2 As FileInfo) As Integer
Return f1.CreationTime.CompareTo(f2.CreationTime)
End Function
[C#]
public int CompareDates(FileInfo fi, FileInfo f2)
{
return f1.CreationTime.CompareTo(f2.CreationTime);
}
You could sort by CreationDate using this code:
[VB]
Array.Sort(files, AddressOf CompareDates)
[C#]
Array.Sort(files, CompareDates);
I love it. This technique takes the sting out of sorting data structures in multiple ways.
Published Friday, April 21, 2006 8:19 AM by KenG
[출처] http://mcwtech.com/CS/blogs/keng/archive/2006/04/21/154.aspx
'웹프로그래밍 > .NET' 카테고리의 다른 글
Visual Studio 2010 과 .NET Framework 4 훈련과정 (0) | 2010.06.24 |
---|---|
[ASP.NET] ASP.NET 3.5 ListView & DataPager 사용법 (0) | 2010.06.14 |
[ASP.NET] 콧수염아저씨와 함께 배워보는 ListView 컨트롤 (0) | 2010.06.14 |
ASP.NET에서 게시판 디자인 받아서 ASP 또는 PHP 같이 작업하려면? (0) | 2010.01.03 |
C# Prepared Statements (0) | 2008.11.09 |
- Total
- Today
- Yesterday
- Make Use Of
- How to geek
- 인터넷 통계정보 검색시스템
- 트위터 공유 정보모음
- 웹표준KR
- 치우의 컴맹탈출구
- Dev. Cheat Sheets
- w3schools
- Dev. 조각들
- ASP Ajax Library
- CSS Tricks
- WebResourcesDepot
- jQuery Selectors Tester
- DeveloperSnippets
- Smashing Magazine
- Nettuts+
- devListing
- 웹 리소스 사이트(한)
- Mobile tuts+
- Dream In Code
- Developer Tutorials
- CSS3 Previews
- 자북
- 안드로이드 사이드
- Code Visually
- Code School
- SQLer.com
- 무료 파워포인트 템플릿
- iconPot
- Free PowerPoint Templates
- Design Bombs
- Web Designer Wall
- 1st Webdesigner
- Vandelay Design
- 무료 벡터 이미지 사이트들
- Tripwire Magazine
- Web TrendSet
- WebMonkey
- 윤춘근 프리젠테이션 디자이너 블로그
- cz.cc 무료 DNS
- [웹하드] MediaFire
- [웹하드] DivShare
- 한컴 인터넷 오피스
- classic asp
- 워드프레스
- Prototype
- Chrome
- JSON
- laravel
- sencha touch
- javascript
- iis
- 안드로이드
- Android
- nginx
- IE
- Linux
- CSS
- Docker
- Mac
- git
- Wordpress
- Debug
- iphone
- ASP
- centos
- nodejs
- PHP
- JQuery
- mssql
- API
- 한글
- IOS
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |