获取字母数字值的Max()

时间:2011-09-01 09:44:01

标签: c# .net linq max

我有一个包含ID的字典,这是字母数字(例如a10a10& d10a9),我想从中获得最大的ID,意思是9< 10<一个...

当我使用以下代码时,d10a9为MAX,因为9在10之前排序

var lsd = new Dictionary<string, string>();
lsd.Add("a", "d10a10");
lsd.Add("b", "d10a9");
string max = lsd.Max(kvp => kvp.Value);

如何获取最长字符串组合的ID的最大值?

6 个答案:

答案 0 :(得分:3)

我想你可能会尝试推出自己的IComparer<string>

class HumanSortComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        // your human sorting logic here
    }
}

用法:

var last = collection.OrderBy(x => x.Value, new HumanSortComparer()).LastOrDefault();
if (last != null)
    string max = last.Value;

答案 1 :(得分:2)

这就像一个假设ID始终以“d10a”开头的魅力:

int max = lsd.Max(kvp => Convert.ToInt32(kvp.Value.Substring(4)));
Console.Write(string.Format("d10a{0}", max));

答案 2 :(得分:1)

一种方法是做到这一点

string max =lsd.Where(kvp=>kvp.Value.Length==lsd.Max(k=>k.Value.Length)).Max(kvp => kvp.Value);

但是我认为这种方法会评估每个项目的最大长度,因此您最好先将其提取到变量

int maxLength=lsd.Max(kvp=>kvp.Value.Length);
string max = lsd.Where(kvp=>kvp.Value.Length == maxLength).Max(kvp => kvp.Value);

如果你要在那里有空字符串,你可能还需要执行空检查

int maxLength=lsd.Max(kvp=>(kvp.Value??String.Empty).Length);
string max = lsd.Where(kvp=>(kvp.Value??String.Empty).Length == maxLength).Max(kvp => kvp.Value);

或者将您的字符串视为Base36编号,并将max转换为long函数,然后再转换回来获取最大字符串。

string max =lsd.Max(tvp=>tvp.Value.FromBase36()).ToBase36();

public static class Base36 {

  public static long FromBase36(this string src) {
    return src.ToLower().Select(x=>(int)x<58 ? x-48 : x-87).Aggregate(0L,(s,x)=>s*36+x);
  }

  public static string ToBase36(this long src) {
    StringBuilder result=new StringBuilder();
    while(src>0) {
      var digit=(int)(src % 36);
      digit=(digit<10) ? digit+48 :digit+87;
      result.Insert(0,(char)digit);
      src=src / 36;
      }
    return result.ToString();
   }
}

最后只是Agregate扩展方法而不是Max,因为这可以让你做所有的比较逻辑......

lsd.Agregate(string.Empty,(a,b)=> a.Length == b.Length ? (a>b ? a:b) : (a.Length>b.Length ? a:b));

这可能没有空检查,但您可以轻松添加它们。

答案 3 :(得分:0)

我想如果你这样做了:

var max = lsd.OrderByDescending(x => x.Value)
    .GroupBy(x => x.Value.Length)
    .OrderByDescending(x => x.Key)
    .SelectMany(x => x)
    .FirstOrDefault();

它可能会给你你想要的东西。

答案 4 :(得分:0)

您需要StringComparer.OrdinalIgnoreCase。

无需使用linq,这样做的功能非常简单。 复杂性当然是O(n)。

    public static KeyValuePair<string, string> FindMax(IEnumerable<KeyValuePair<string, string>> lsd)
    {
        var comparer = StringComparer.OrdinalIgnoreCase;
        var best = default(KeyValuePair<string, string>);
        bool isFirst = true;
        foreach (KeyValuePair<string, string> kvp in lsd)
        {
            if (isFirst || comparer.Compare(kvp.Value, best.Value) > 0)
            {
                isFirst = false;
                best = kvp;
            }
        }
        return best;
    }

答案 5 :(得分:0)

好的 - 我认为您需要先将每个键转换为一系列字符串和数字 - 因为您需要整数才能确定比较。然后你实现了一个IComparer - 我用你的两个输入字符串以及其他几个输入字符串对它进行了测试,看起来它可以做你想要的。表现可能会有所改善 - 但我正在集思广益!

创建此类:

public class ValueChain
{
  public readonly IEnumerable<object> Values;
  public int ValueCount = 0;

  private static readonly Regex _rx = 
    new Regex("((?<alpha>[a-z]+)|(?<numeric>([0-9]+)))", 
      RegexOptions.Compiled | RegexOptions.IgnoreCase);

  public ValueChain(string valueString)
  {
    Values = Parse(valueString);
  }

  private IEnumerable<object> Parse(string valueString)
  {
    var matches = _rx.Matches(valueString);
    ValueCount = matches.Count;

    foreach (var match in matches.Cast<Match>())
    {
      if (match.Groups["alpha"].Success)
        yield return match.Groups["alpha"].Value;
      else if (match.Groups["numeric"].Success)
        yield return int.Parse(match.Groups["numeric"].Value);
    }
  }
}

现在这个比较器:

public class ValueChainComparer : IComparer<ValueChain>
{
  private IComparer<string> StringComparer;
  public ValueChainComparer()
    : this(global::System.StringComparer.OrdinalIgnoreCase)
  {

  }

  public ValueChainComparer(IComparer<string> stringComparer)
  {
    StringComparer = stringComparer;
  }

  #region IComparer<ValueChain> Members

  public int Compare(ValueChain x, ValueChain y)
  {
    //todo: null checks
    int comparison = 0;
    foreach (var pair in x.Values.Zip
      (y.Values, (xVal, yVal) => new { XVal = xVal, YVal = yVal }))
    {
      //types match?
      if (pair.XVal.GetType().Equals(pair.YVal.GetType()))
      {
        if (pair.XVal is string)
          comparison = StringComparer.Compare(
            (string)pair.XVal, (string)pair.YVal);
        else if (pair.XVal is int) //unboxing here - could be changed
          comparison = Comparer<int>.Default.Compare(
            (int)pair.XVal, (int)pair.YVal);
        if (comparison != 0)
          return comparison;
      }
      else  //according to your rules strings are always greater than numbers.
      {
        if (pair.XVal is string)
          return 1;
        else
          return -1;
      }
    }

    if (comparison == 0) //ah yes, but were they the same length?
    {
      //whichever one has the most values is greater
      return x.ValueCount == y.ValueCount ? 
        0 : x.ValueCount < y.ValueCount ? -1 : 1;
    }

    return comparison;
  }

  #endregion
}

现在,您可以使用OrderByDescendingIEnumerable<ValueChain>上的FirstOrDefault获取最大值:

[TestMethod]
public void TestMethod1()
{
  List<ValueChain> values = new List<ValueChain>(new [] 
                            {
                              new ValueChain("d10a9"),
                              new ValueChain("d10a10")
                            });

  ValueChain max = 
    values.OrderByDescending(v => v, new ValueChainComparer()).FirstOrDefault();
}

因此,您可以使用它来对字典中的字符串值进行排序:

var maxKvp = lsd.OrderByDescending(kvp => new ValueChain(kvp.Value), 
                      new ValueChainComparer()).FirstOrDefault();