how to sort alphanumeric in ascending order using c# window application
eg. p-1,p-2,p-3,.....,p-10,p-11.. like this.
shouldn't come p-1,p-11,p-12,.....,p-2,p-3 like this.
Hai ,
In my project am working on report viewer, Here I want to fetch database values in table property(Details Columns), During this process I want to automatically expand or shrink the database values in that table property depending upon the database characters.
Please someone help to overcome this issue. Its really urgent to finish.
Thanks in advance.
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 2, 2016, 9:18 AM EST
Try this;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
{
class Program
{
static void Main(string[] args)
{
string[] highways = new string[]
{
"P-1",
"P-2",
"P-10",
"P-11"
};
//
// We want to sort a string array called highways in an
// alphanumeric way. Call the static Array.Sort method.
//
Array.Sort(highways, new AlphanumComparatorFast());
//
// Display the results
//
foreach (string h in highways)
{
Console.WriteLine(h);
}
Console.Read();
}
}
{
public int Compare(object x, object y)
{
string s1 = x as string;
if (s1 == null)
{
return 0;
}
string s2 = y as string;
if (s2 == null)
{
return 0;
}
int len2 = s2.Length;
int marker1 = 0;
int marker2 = 0;
while (marker1 < len1 && marker2 < len2)
{
char ch1 = s1[marker1];
char ch2 = s2[marker2];
char[] space1 = new char[len1];
int loc1 = 0;
char[] space2 = new char[len2];
int loc2 = 0;
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do
{
space1[loc1++] = ch1;
marker1++;
{
ch1 = s1[marker1];
}
else
{
break;
}
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
{
space2[loc2++] = ch2;
marker2++;
{
ch2 = s2[marker2];
}
else
{
break;
}
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
// Otherwise, if we have strings, compare them alphabetically.
string str1 = new string(space1);
string str2 = new string(space2);
{
int thisNumericChunk = int.Parse(str1);
int thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
}
{
return result;
}
}
return len1 - len2;
}
}
}
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 2, 2016, 9:20 AM EST
Visit this link for the solution:
sridevi u50 reputationPosted Mar 2, 2016, 11:06 PM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 3, 2016, 4:45 AM EST
sridevi u50 reputationPosted Mar 3, 2016, 10:47 PM EST
sridevi u50 reputationPosted Mar 4, 2016, 3:09 AM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 4, 2016, 6:24 AM EST
Amatya AgyeyRank #1,635 · 944 reputationPosted Mar 8, 2016, 7:31 PM EST
sridevi u50 reputationPosted Mar 9, 2016, 3:27 AM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 9, 2016, 11:33 AM EST
Try this :
{
AutoCompleteStringCollection oAutoCompleteStringCollection = new AutoCompleteStringCollection();
List
DataTable dt = new DataTable();
dt.Columns.Add("SSN", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("ADDR", typeof(string));
dt.Columns.Add("AGE", typeof(int));
// Showing how to set Primary Key(s) in a Data table (Although it's not compulsory to have one)
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns[0];
dt.PrimaryKey = keys;
dt.Rows.Add("203456876", "John", "12 Main Street, Newyork, NY", 15);
dt.Rows.Add("203456877", "SAMMY", "13 Main Ct, Newyork, NY", 25);
dt.Rows.Add("203456878", "Elan", "14 Main Street, Newyork, NY", 35);
dt.Rows.Add("203456879", "Smith", "12 Main Street, Newyork, NY", 45);
dt.Rows.Add("203456880", "SAM", "345 Main Ave, Dayton, OH", 55);
dt.Rows.Add("203456881", "Sue", "32 Cranbrook Rd, Newyork, NY", 65);
dt.Rows.Add("203456882", "Winston", "1208 Alex St, Newyork, NY", 65);
dt.Rows.Add("203456883", "Smith", "126 Province Ave, Baltimore, NY", 85);
dt.Rows.Add("203456884", "SAM", "126 Province Ave, Baltimore, NY", 95);
foreach (DataRow dr in dt.Rows)
{
NameList.Add(dr[1].ToString()); // 1 stands for the "Name" column
}
// Converting NameList collection into Array so that we could easily sort
string[] highways = NameList.ToArray();
Array.Sort(highways, new AlphanumComparatorFast());
foreach (string h in highways)
{
oAutoCompleteStringCollection.Add(h);
}
textBox1.AutoCompleteCustomSource = oAutoCompleteStringCollection;
}
{
public int Compare(object x, object y)
{
string s1 = x as string;
if (s1 == null)
{
return 0;
}
string s2 = y as string;
if (s2 == null)
{
return 0;
}
int len2 = s2.Length;
int marker1 = 0;
int marker2 = 0;
while (marker1 < len1 && marker2 < len2)
{
char ch1 = s1[marker1];
char ch2 = s2[marker2];
char[] space1 = new char[len1];
int loc1 = 0;
char[] space2 = new char[len2];
int loc2 = 0;
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do
{
space1[loc1++] = ch1;
marker1++;
{
ch1 = s1[marker1];
}
else
{
break;
}
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
{
space2[loc2++] = ch2;
marker2++;
{
ch2 = s2[marker2];
}
else
{
break;
}
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
// Otherwise, if we have strings, compare them alphabetically.
string str1 = new string(space1);
string str2 = new string(space2);
{
int thisNumericChunk = int.Parse(str1);
int thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
}
{
return result;
}
}
return len1 - len2;
}
}
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 9, 2016, 11:36 AM EST
Attaching solution. If it helps you, please mark the answer accepted.
sridevi u50 reputationPosted Mar 9, 2016, 8:14 PM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 10, 2016, 5:06 AM EST
I entered S1, S10, S11, S2, S9 values into DataTable. It's getting sorted properly 'AutoCompleteStringCollection'. BUT when form gets loaded, don't why sorting of that collection is getting changed. Here is the screenshot:
Hemant SrivastavaRank #581 · 4.2K reputationPosted Mar 10, 2016, 5:09 AM EST
However sorting gets changed at run time don't know yet why? See the screenshot below
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts