.NET does not support alphanumeric sort of arrays. (I hope that changes
soon!) Meanwhile, here is a solution.
You could use this in your listbox by sorting the items in an array and
then
adding the array items from 0 to length to your listbox.
1. Create a custom comparer class that implements IComparer:
using System;
using System.IO;
namespace Test1CSharp
{
/// <summary>
/// Lacey Orr
/// 29 June 2005
/// Alpha-numeric sorting solution.
/// </summary>
public class AlphaNumCompare : System.Collections.IComparer
{
public int Compare(Object a1, Object b1)
{
//In my case, I compared Directory objects. So I took
out
// the filenames / foldernames from the parameter
objects and
// passed those to the sort.
//The string variables to compare
string a = "";
string b = "";
//Is a1 a FileInfo?
if (a1.GetType() == System.Type.GetType("FileInfo"))
a = ((FileInfo)a1).Name;
else
a = a1.ToString();
//Is b1 a FileInfo?
if (b1.GetType() == System.Type.GetType("FileInfo"))
b = ((FileInfo)b1).Name;
else
b = b1.ToString();
return CompareAlphaNum(a, b);
}
// CompareAlphaNum: Does an alphabetic sort.
private static int CompareAlphaNum (string a, string b)
{
//Do a quick check for empty strings. If one
string
is empty, then we
// can get out without doing any work.
if (a.Length == 0 && b.Length > 0)
return -1;
else if (a.Length > 0 && b.Length == 0)
return 1;
else if (a.Length == 0 && b.Length == 0)
return 0;
//The order of chars - make this however you want.
string strNums = "0123456789";
string strSortOrder = "
.!#$%&'()*+,-/:;<=>?@[]^_{}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
//Variables for comparing
bool aSmaller = true;
bool isFound = false;
int intIndex = 0;
// intLength determines the number of times to
loop.
We will loop
// until we hit the end of the shorter string -
a
or b.
int intLength = (a.Length < b.Length? a.Length:
b.Length);
string strNumA = "";
string strNumB = "";
int numA = 0;
int numB = 0;
int j = 0;
int k = 0;
//Do the compare while we are not at the end of
either string and haven't found
// the result.
while (!isFound && intIndex < intLength)
{
// if we are dealing with numbers, then sort
the numbers numerically
if (strNums.IndexOf(a[intIndex]) > -1 &&
strNums.IndexOf(b[intIndex]) > -1)
{
//Get all the numbers in string A until
we hit a non-number
j = intIndex;
while (j < a.Length &&
strNums.IndexOf(a[j]) > -1)
{
strNumA += a[j].ToString();
j++;
}
//Get all the numbers in string B until
we hit a non-number
k = intIndex;
while (k < b.Length &&
strNums.IndexOf(b[k]) > -1)
{
strNumB += b[k].ToString();
k++;
}
numA = Convert.ToInt32(strNumA);
numB = Convert.ToInt32(strNumB);
if (numA < numB) // a is before b in
sort order; a < b
return -1;
else if (numA > numB) // b is before a
in sort order; a > b
return 1;
else if (numA == numB)
{
//The numbers are the same.
Remove the number part from the strings
// and compare the remainder
of
the string.
return
CompareAlphaNum(a.Substring(strNumA.Length, a.Length-strNumA.Length),
b.Substring(strNumB.Length, b.Length-strNumB.Length));
}
}
else
{
if (strSortOrder.IndexOf(b[intIndex]) <
strSortOrder.IndexOf(a[intIndex]))
{
// If string a < b in a sort,
then
we're done
aSmaller = false;
isFound = true;
}
else if
(strSortOrder.IndexOf(b[intIndex]) > strSortOrder.IndexOf(a[intIndex]))
{
// If string a > b in a sort,
then
we're done
aSmaller = true;
isFound = true;
}
else if (( b.Length < a.Length) &&
(intIndex == intLength - 1))
{
// If the strings are equal up to
the length-th char but a is longer,
// then we're done.
aSmaller = false;
isFound = true;
}
else
{
// Otherwise, keep sorting
intIndex ++;
}
}
}
if ((a.Length == b.Length) && !isFound)
return 0; //strings are the same.
else if (aSmaller)
return -1; // a is before b in sort order; a
< b
else
return 1; // b is before a in sort order; ; a
}
}
}
2. Use the custom class using Array.Sort(myArray, new MyCompareClass()).
a. Add a new web page
b. Add:
using System.IO;
using System.Text;
c. Add a label to the form (lblDir) to display the sort
d. In the page load:
private void Page_Load(object sender, System.EventArgs e)
{
//Get files in dir
String strDir = MapPath("~/./");
DirectoryInfo curDir = new DirectoryInfo(strDir);
FileInfo [] fiArray = curDir.GetFiles();
string [] strFilenames = new string[fiArray.Length];
for (int j = 0; j < fiArray.Length; j++)
{
strFilenames[j] = fiArray[j].Name;
}
// Sort files
Array.Sort(strFilenames, new AlphaNumCompare());
//Display files
StringBuilder sbFiles = new StringBuilder();
for (int k = 0; k < strFilenames.Length; k++)
{
sbFiles.Append(strFilenames[k] + "<BR>");
}
lblDir.Text = sbFiles.ToString();
}
Lacey
Federico G. Babelis said:
Hi All:
I have this line of code, but the syntax check in VB.NET 2003 and also in
VB.NET 2005 Beta 2 shows as unknown:
Dim local4 As Byte
Fixed(local4 = AddressOf dest(offset))
CType(local4, Short) = CType(src, Short)
Return
End Fixed
What is the "FIXED and END FIXED" and how the syntax error can be "fixed"
???
Thx,
Federico