■ Array 클래스의 Sort 정적 메소드를 사용해 자연 정렬을 하는 방법을 보여준다.
▶ SortHelper.cs
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
using System.Globalization; namespace TestProject { /// <summary> /// 정렬 헬퍼 /// </summary> public class SortHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비교하기 - Compare(source1, source2, cultureInfo, options) /// <summary> /// 비교하기 /// </summary> /// <param name="source1">소스 문자열 1</param> /// <param name="source2">소스 문자열 2</param> /// <param name="cultureInfo">문화 정보</param> /// <param name="options">비교 옵션</param> /// <returns>비교 결과</returns> public static int Compare(string source1, string source2, CultureInfo cultureInfo, CompareOptions options) { CompareInfo compareInfo = cultureInfo.CompareInfo; int index1 = 0; int index2 = 0; int result = 0; int resultWeight = 0; while(index1 < source1.Length && index2 < source2.Length) { bool isDigit1 = char.IsDigit(source1[index1]); bool isDigit2 = char.IsDigit(source2[index2]); if(isDigit1 != isDigit2) { return compareInfo.Compare(source1, index1, source2, index2, options); } else if(!isDigit1 && !isDigit2) { int nextIndex1 = index1 + 1; int nextIndex2 = index2 + 1; while(nextIndex1 < source1.Length && !Char.IsDigit(source1[nextIndex1])) nextIndex1++; while(nextIndex2 < source2.Length && !Char.IsDigit(source2[nextIndex2])) nextIndex2++; int temporaryResult = compareInfo.Compare(source1, index1, nextIndex1 - index1, source2, index2, nextIndex2 - index2, options); if(temporaryResult != 0) { string section1 = source1.Substring(index1, nextIndex1 - index1); string section2 = source2.Substring(index2, nextIndex2 - index2); if(compareInfo.Compare(section1 + "1", section2 + "2", options) == compareInfo.Compare(section1 + "2", section2 + "1", options)) { return compareInfo.Compare(source1, index1, source2, index2, options); } else if(resultWeight < 1) { result = temporaryResult; resultWeight = 1; } } index1 = nextIndex1; index2 = nextIndex2; } else { char zeroCharacter1 = (char)(source1[index1] - (int)char.GetNumericValue(source1[index1])); char zeroCharacter2 = (char)(source2[index2] - (int)char.GetNumericValue(source2[index2])); int index3 = index1; int index4 = index2; while(index3 < source1.Length && source1[index3] == zeroCharacter1) index3++; while(index4 < source2.Length && source2[index4] == zeroCharacter2) index4++; int resultIfSameLength = 0; do { isDigit1 = index3 < source1.Length && Char.IsDigit(source1[index3]); isDigit2 = index4 < source2.Length && Char.IsDigit(source2[index4]); int number1 = isDigit1 ? (int)char.GetNumericValue(source1[index3]) : 0; int number2 = isDigit2 ? (int)char.GetNumericValue(source2[index4]) : 0; if(isDigit1 && (char)(source1[index3] - number1) != zeroCharacter1) { isDigit1 = false; } if(isDigit2 && (char)(source2[index4] - number2) != zeroCharacter2) { isDigit2 = false; } if(isDigit1 && isDigit2) { if(number1 != number2 && resultIfSameLength == 0) { resultIfSameLength = number1 < number2 ? -1 : 1; } index3++; index4++; } } while(isDigit1 && isDigit2); if(isDigit1 != isDigit2) { return isDigit1 ? 1 : -1; } else if(resultIfSameLength != 0) { return resultIfSameLength; } int index5 = index3 - index1; int index6 = index4 - index2; if(index5 != index6) { return index5 > index6 ? -1 : 1; } else if(zeroCharacter1 != zeroCharacter2 && resultWeight < 2) { result = compareInfo.Compare(source1, index1, 1, source2, index2, 1, options); resultWeight = 2; } index1 = index3; index2 = index4; } } if(index1 < source1.Length || index2 < source2.Length) { return index1 < source1.Length ? 1 : -1; } else if(result != 0) { return result; } return 0; } #endregion #region 비교하기 - Compare(source1, source2) /// <summary> /// 비교하기 /// </summary> /// <param name="source1">소스 문자열 1</param> /// <param name="source2">소스 문자열 2</param> /// <returns>비교 결과</returns> public static int Compare(string source1, string source2) { return Compare(source1, source2, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase); } #endregion } } |
▶ Program.cs
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 31 32 |
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string[] filePathArray = Directory.GetFiles("D:\\temp"); Array.Sort(filePathArray, SortHelper.Compare); foreach(string filePath in filePathArray) { Console.WriteLine(filePath); } } #endregion } |