c# 숫자 알파벳 혼합 정렬 비교기
2488 단어 c#정렬문자열 정렬알파벳 혼합 정렬문자열 정렬 비교기
class StringComparer : IComparer
{
public bool MatchCase { get; }
public StringComparer(bool matchCase) { MatchCase = matchCase; }
private int CharCompare(char a, char b, bool matchCase)
{
char _a = char.MinValue, _b = char.MinValue;
if (matchCase) { _a = a; _b = b; }
else { _a = char.ToUpper(a); _b = char.ToUpper(b); }
if (_a > _b) return 1;
if (_a < _b) return -1;
return 0;
}
public int Compare(string x, string y)
{
int len;
if (x.Length > y.Length) len = x.Length;
else len = y.Length;
string numericx = "";
string numericy = "";
for (int i = 0; i < len; i++)
{
char cx = char.MinValue;
char cy = char.MinValue;
if (i < x.Length) cx = x[i];
if (i < y.Length) cy = y[i];
if (cx >= 48 && cx <= 57) numericx += cx;
if (cy >= 48 && cy <= 57) numericy += cy;
if (i == len - 1)
{
if (numericx.Length > 0 && numericy.Length > 0)
{
if (decimal.Parse(numericx) < decimal.Parse(numericy)) return -1;
if (decimal.Parse(numericx) > decimal.Parse(numericy)) return 1;
}
return CharCompare(cy, cy, MatchCase);
}
if ((cx >= 48 && cx <= 57) && (cy >= 48 && cy <= 57)) continue;
if (numericx.Length > 0 && numericy.Length > 0)
{
if (decimal.Parse(numericx) < decimal.Parse(numericy)) return -1;
if (decimal.Parse(numericx) > decimal.Parse(numericy)) return 1;
}
if (CharCompare(cx, cy, MatchCase) == 0) continue;
return CharCompare(cx, cy, MatchCase);
}
return 0;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Task를 사용하여 비동기식 작업을 수행하는 방법라인이 완성된 후에 이 라인을 다시 시작할 수 없습니다.반대로 조인(Join)만 결합할 수 있습니다 (프로세스가 현재 라인을 막습니다). 임무는 조합할 수 있는 것이다. 연장을 사용하여 그것들을 한데 연결시키는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.