Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

How to find greatest among all alphanumeric strings in c#.

Ex.
C#
string rev1 = "B1";
       string rev2 = "C2";
       string rev3 = "D1";
       string rev4 = "GH1";
       string rev5 = "Ik1";
       string rev6 = "BT4";
       string rev7 = "NK";

among all the above string which one is greatest how to find.

Thanks in advance

What I have tried:

I have tried the below code, but it seems very long and lengthy , is there any another way to do this task. please suggest me.
C#
string rev1 = "B1";
      string rev2 = "C2";
      string rev3 = "D1";
      string rev4 = "GH1";
      string rev5 = "Ik1";
      string rev6 = "BT4";
      string rev7 = "NK";

     int a = string.Compare(rev1, rev2);
     int b, c, d,  f, g, h, i,j,k,l,m;
      if(a==-1)
      {
          int b = string.Compare(rev2, rev3);
          if(b==-1)
          {
              int c = string.Compare(rev3,rev4);
          }
          else
          {
              int c = string.Compare(rev2,rev3);
          }
      }
      else
      {
          int b = string.Compare(rev1,rev3);
          if(b==-1)
          {
              int d = string.Compare(rev3,rev4);
          }
      }
Posted
v2
Comments
Andy Lanng 15-Mar-16 5:34am    
Define "Greatest".
Mukesh Pr@sad 15-Mar-16 5:41am    
i didn't get you, can you please elaborate it.
Kornfeld Eliyahu Peter 15-Mar-16 5:44am    
When you talking about numbers, than it is clear what 'greatest' means, but in case of alphanumeric we have letters involved too, so you may speak of order, but you are talking about 'greatest'!
What do you mean, what are the rules? Which is the greatest 'C2' or 'BT4'? Why?
Mukesh Pr@sad 15-Mar-16 5:47am    
In case of C2 and BT4 , BT4 should be greatest as it contains "T"
Kornfeld Eliyahu Peter 15-Mar-16 5:53am    
In this case, maybe the best thing is to handle those strings like a number of base 36 (good only if there is no upper and lower case issue)...

Create an array of your strings:
C#
string[] data = new string[] { "B1", "C2", "D1", "GH1", "Ik1", "BT4", "NK" };

Now, you can access them in a loop via a numeric index instead of trying to work with named variables all the time. This will dramatically reduce your code!
And almost certainly, you can use the standard .NET framework methods to make it trivial to get the greatest - it's the last one in a sorted collection:
C#
string[] data = new string[] { "B1", "C2", "D1", "GH1", "Ik1", "BT4", "NK" };
Array.Sort(data);
Console.WriteLine(data[data.Length - 1]);


"If I understood correctly OP, it is about numbers of base 26...not only simple sort..."
Ah.
Still pretty simple!
C#
string[] data = new string[] { "B1", "C2", "D1", "GH1", "Ik1", "BT4", "NK" };
data = data.OrderBy(s => s.ToUpper().Max()).ToArray();
Console.WriteLine(data[data.Length - 1]);
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 15-Mar-16 5:57am    
If I understood correctly OP, it is about numbers of base 26...not only simple sort...
Andy Lanng 15-Mar-16 6:05am    
maybe even base36
Kornfeld Eliyahu Peter 15-Mar-16 6:09am    
Of course - I counted only the letter and skipped the numerics...
OriginalGriff 15-Mar-16 6:08am    
Still pretty simple! :laugh:
Ok - I think we are all still unclear as to what you exactly need, but there is a way to easily sort the items once you know how to evaluate the "Greatest" given only 2 strings to compare.

C#
class CustomStringComparer : IComparer<string>
{
    private readonly IComparer<string> _baseComparer;
    public CustomStringComparer(IComparer<string> baseComparer)
    {
        _baseComparer = baseComparer;
    }

    public int Compare(string x, string y)
    {
        // Here you can compare x & y in whatever way you like.  

        //return -1 if x is less great than y
        //return 0 if x is the same greatness as y
        //return 1 if x is more great than y
        
    }
}
</string></string></string>


Now that you have a IComparer class, you can easily sort your array just as OG suggested:

C#
var data = new string[]{ "B1", "C2", "D1", "GH1", "Ik1", "BT4", "NK" };
var comparer = new CustomStringComparer(StringComparer.CurrentCulture);
Array.Sort(data , comparer);


This will sort the array in order of greatness. If you want the greatest then you can just the first item of the sorted list

C#
return data[0];


hope that helps ^_^
 
Share this answer
 
Belolw is the solution what I got.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       string rev1 = "B1";
       string rev2 = "C2";
       string rev3 = "D1";
       string rev4 = "GH1";
       string rev5 = "Ik1";
       string rev6 = "BT4";
       string rev7 = "NK";
       String[] words = { "b1", "c2", "d1", "gh1", "ik4",
                        "bt4", "nk"};
       Array.Sort(words, 0, 6);
       DisplayValues(words);




   }
   public static void DisplayValues(String[] arr)
   {
       for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0);
             i++)
       {
           Console.WriteLine("   [{0}] : {1}", i, arr[i]);
       }
       Console.WriteLine();
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900