Skip to content

Instantly share code, notes, and snippets.

@7shi
Created February 2, 2011 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/807274 to your computer and use it in GitHub Desktop.
Save 7shi/807274 to your computer and use it in GitHub Desktop.
compare strings containing numbers
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