Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6391987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:45:16+00:00 2026-05-25T03:45:16+00:00

I have a dictionary containg ID which are alphanumeric (e.g. a10a10 & d10a9) from

  • 0

I have a dictionary containg ID which are alphanumeric (e.g. a10a10 & d10a9) from which I want the biggest ID, meaning 9 < 10 < a …

When I use the following code, d10a9 is MAX since 9 is sorted before 10

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

How can I get the Max value of the IDs with the Longest string combined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:45:17+00:00Added an answer on May 25, 2026 at 3:45 am

    One way would be to do this

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

    however I think that this method would evalute the max length for each item so you may be better to extract it to a variable first

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

    If you are going to have null strings in there you may need to perform null checks too

    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);
    

    Alternatively treat your string as Base36 number and convert to long for the max function and then convert back again to get the max string.

    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();
       }
    }
    

    Finally just just the Agregate extension method instead of Max as this lets you do all the comparison logic….

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

    This could doesn’t have null checks but you easily add them in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic dictonary which is templated in the following manner: Dictionary<object, IList<ISomeInterface>>
I have a dictionary dict2 which I want to iter through and remove all
I have a Dictionary where I hold data for movieclips, and I want the
I have a dictionary of dictionaries with the following structure: 1:{'Amendment': '1', 'status': 'Stadf\xc3\xa6stet',
I have used AutoCompleteBox usercontrol. I have a dictionary of type Dictionary<int,string> which contains
I have a Dictionary> in c# Dictionary<string,List<string>> l_dictRawData; which contains the values are :
Consider I have the following dictionary: dic={} dic[var1]=val1 dic[var2]=val2 Now I have another object
Say I have a Dictionary object which contains many instances of an object called
I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex
in my Class I have a static dictionary of strings object which contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.