Created
February 2, 2011 05:17
compare strings containing numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
public class NumberStringComparer : IComparer<string> | |
{ | |
public int Compare(string x, string y) | |
{ | |
string[] xs = Split(x), ys = Split(y); | |
int len = Math.Min(xs.Length, ys.Length); | |
for (int i = 0; i < len; i++) | |
{ | |
string xx = xs[i], yy = ys[i]; | |
if (xx.Length < yy.Length) return -1; | |
if (xx.Length > yy.Length) return 1; | |
int cmp = xx.CompareTo(yy); | |
if (cmp != 0) return cmp; | |
} | |
if (xs.Length < ys.Length) return -1; | |
if (xs.Length > ys.Length) return 1; | |
return 0; | |
} | |
public static string[] Split(string s) | |
{ | |
var ret = new List<string>(); | |
var num = new StringBuilder(); | |
foreach (char ch in s) | |
{ | |
if ('0' <= ch && ch <= '9') | |
num.Append(ch); | |
else if ('0' <= ch && ch <= '9') | |
num.Append((char)('0' + (ch - '0'))); | |
else | |
{ | |
if (num.Length > 0) | |
{ | |
ret.Add(num.ToString()); | |
num.Length = 0; | |
} | |
ret.Add(ch.ToString()); | |
} | |
} | |
if (num.Length > 0) ret.Add(num.ToString()); | |
return ret.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment