Det er lang tid siden jeg sidst har kodet saadan noget.
Men her et et bud:
using System;
using System.Collections.Generic;
namespace E
{
public class PhpKSort : IComparer<string>
{
private const int OFFSET = 1000000;
private int Value(char c)
{
if('0' <= c && c <= '9')
{
return 3 * OFFSET + c;
}
else if('A' <= c && c <= 'Z')
{
return 1 * OFFSET + c;
}
else if('a' <= c && c <= 'z')
{
return 2 * OFFSET + c;
}
else
{
throw new ArgumentException(c + " does not have an associated value");
}
}
public int Compare(string x, string y)
{
int ix = 0;
while(ix < x.Length || ix < y.Length)
{
if(ix >= x.Length) return -1;
if(ix >= y.Length) return 1;
int xval = Value(x[ix]);
int yval = Value(y[ix]);
if(xval < yval)
{
return -1;
}
else if(xval > yval)
{
return 1;
}
else
{
ix++;
}
}
return 0;
}
}
public class Program
{
public static void Main(string[] args)
{
SortedDictionary<string, string> sdict = new SortedDictionary<string, string>(new PhpKSort());
sdict.Add("1", "7");
sdict.Add("b", "4");
sdict.Add("a", "3");
sdict.Add("d", "5");
sdict.Add("A", "1");
sdict.Add("3", "9");
sdict.Add("Q", "2");
sdict.Add("e", "6");
sdict.Add("11", "7");
sdict.Add("1b", "4");
sdict.Add("1a", "3");
sdict.Add("1d", "5");
sdict.Add("1A", "1");
sdict.Add("13", "9");
sdict.Add("1Q", "2");
sdict.Add("1e", "6");
sdict.Add("b1", "7");
sdict.Add("bb", "4");
sdict.Add("ba", "3");
sdict.Add("bd", "5");
sdict.Add("bA", "1");
sdict.Add("b3", "9");
sdict.Add("bQ", "2");
sdict.Add("be", "6");
sdict.Add("A1", "7");
sdict.Add("Ab", "4");
sdict.Add("Aa", "3");
sdict.Add("Ad", "5");
sdict.Add("AA", "1");
sdict.Add("A3", "9");
sdict.Add("AQ", "2");
sdict.Add("Ae", "6");
foreach(KeyValuePair<string, string> entry in sdict)
{
Console.WriteLine("{0} -> {1}", entry.Key, entry.Value);
}
Console.ReadKey();
}
}
}