Reputation: 575
I am creating a C# Console app which is Replacing a Hyphen for Zeros to complete the maximum length of my String from an identification card "123456-72"and I am facing a hard time when have to sort in my array.
I would like sort the first digit or character from this "123456-72" as well in some cases I need to sort from the first two digit "391234-56".
This example is working fine, but only for first character. I need to Example:
class IdentificationNumber {
private string IDNumber;
private int Customertype;
private string check;
private string pad;
private string longFormat;
SortedList GroupID = new SortedList();
public IdentificationNumber(string IDNumber) {
this.IDNumber= IDNumber;
}
public string getLongFormat() {
var ReplaceHyp = IDNumber.Replace("-", "");
int Customertype= Int32.Parse(IDNumber.Substring(0,2));
//Array
//GroupID .Add(1,"Blue");
//GroupID .Add(2,"Blue");
GroupID .Add(38,"White");
GroupID .Add(39,"Blue");
pad="";
check = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype)).ToString();
Console.WriteLine(Customertype);
Console.WriteLine(check);
switch (check) {
case("White"):
longFormat= ReplaceHyp.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp.Length),'0')+ReplaceHyp.Substring(6,(ReplaceHyp.Length-6));
break;
case("Blue"):
longFormat= ReplaceHyp.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp.Length),'0')+ReplaceHyp.Substring(7,(ReplaceHyp.Length-7));
break;
}
return longFormat;
}
}
Any solution or suggestion?
Upvotes: 1
Views: 585
Reputation: 15008
Here is a skeleton of the comparator method you might need:
public static int CompareStrings(string s1, string s2)
{
int Customertype1 = Int32.Parse(s1.Substring(0,2));
int Customertype2 = Int32.Parse(s2.Substring(0,2));
string check1 = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype1)).ToString();
string check2 = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype2)).ToString();
if (Customertype1 > Customertype2)
return 1;
if (Customertype1 < Customertype2)
return -1;
else
{
var ReplaceHyp1 = s1.Replace("-", "");
switch (check1) {
case("White"):
longFormat1 = ReplaceHyp1.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp1.Length),'0')+ReplaceHyp1.Substring(6,(ReplaceHyp1.Length-6));
break;
case("Blue"):
longFormat1 = ReplaceHyp1.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp1.Length),'0')+ReplaceHyp1.Substring(7,(ReplaceHyp1.Length-7));
break;
}
var ReplaceHyp2 = s2.Replace("-", "");
switch (check2) {
case("White"):
longFormat2 = ReplaceHyp2.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp2.Length),'0')+ReplaceHyp2.Substring(6,(ReplaceHyp2.Length-6));
break;
case("Blue"):
longFormat2 = ReplaceHyp2.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp2.Length),'0')+ReplaceHyp2.Substring(7,(ReplaceHyp2.Length-7));
break;
}
return stringCompare(longFormat1, longFormat2);
}
}
This code badly needs to be refactored! Depending on your exact needs, I think that the checks of Customertype1/2
can be removed.
Upvotes: 1