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 SrivastavaPosted Mar 10, 2016, 10:09 AM
However sorting gets changed at run time don't know yet why? See the screenshot below
Hemant SrivastavaPosted Mar 10, 2016, 10:06 AM
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:
sridevi uPosted Mar 10, 2016, 1:14 AM
Hemant SrivastavaPosted Mar 9, 2016, 4:36 PM
Attaching solution. If it helps you, please mark the answer accepted.
Hemant SrivastavaPosted Mar 9, 2016, 4:33 PM
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;
}
}
sridevi uPosted Mar 9, 2016, 8:27 AM
Amatya AgyeyPosted Mar 9, 2016, 12:31 AM
Hemant SrivastavaPosted Mar 4, 2016, 11:24 AM
sridevi uPosted Mar 4, 2016, 8:09 AM
sridevi uPosted Mar 4, 2016, 3:47 AM
Hemant SrivastavaPosted Mar 3, 2016, 9:45 AM
sridevi uPosted Mar 3, 2016, 4:06 AM
Hemant SrivastavaPosted Mar 2, 2016, 2:20 PM
Visit this link for the solution:
Hemant SrivastavaPosted Mar 2, 2016, 2:18 PM
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;
}
}
}