471,357 Members | 1,145 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Listbox numerical sort

Hi,

I have a bit of a problem with a sort procedure I need to do. I have a
list of items in a listbox, eg:-

2.3%<A other text here>
-4%<B other text here>
10%<C other text here>
-9.3%<D other text here>
22%<E other text here>

How do I sort these listbox items in numerical descending order to:

22%<E other text here>
10%<C other text here>
2.3%<A other text here>
-4%<B other text here>
-9.3%<D other text here>

Thanks,
Alex

Jul 21 '05 #1
3 2849
Hi Ali,

Use Array.Sort with a custom class or struct of your choice that implements the kind of sorting you need.

The code sample below tells Array.Sort to use the sorting implemented in SortClass. SortClass is designed purely for sorting. All it does is stripping away the number in front and uses the standard numeric comparison on that number. Reverse the result of the sorting or change the sort procedure to do it for you.

protected override void OnLoad(EventArgs e)
{
string[] strings = {"2.3%<A other text here>",
"-4%<B other text here>",
"10%<C other text here>",
"-9.3%<D other text here>",
"22%<E other text here>"};

Array.Sort(strings, new SortClass());
Array.Reverse(strings);

listBox1.Items.AddRange(strings);
}

public class SortClass : IComparer
{
public int Compare(object x, object y)
{
if(!(x is String) || !(y is String))
throw new InvalidCastException("object is not of type string");

string a = (string)x;
string b = (string)y;

int i = a.IndexOf('%');
int j = b.IndexOf('%');

if(i < 1 || j < 1)
throw new FormatException("string is not of expected format");

a = a.Substring(0, i);
b = b.Substring(0, j);

double d = 0;
double e = 0;

if(!double.TryParse(a, NumberStyles.Float, CultureInfo.InvariantCulture, out d)
|| !double.TryParse(b, NumberStyles.Float, CultureInfo.InvariantCulture, out e))
throw new FormatException("string is not of expected format");

return d.CompareTo(e);
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
Hi Morten,

Thanks! I'm a VB.NET coder (and a new one as well) - can this code be
translated to VB?

Alex

Jul 21 '05 #3
Not sure about VB, but the VB.Net code should be almost identical other that syntactical differences. Don't know enough VB.Net to try to write code though.
On Thu, 09 Jun 2005 12:02:14 +0200, Ali Chambers <in**@alexchambers.co.uk> wrote:
Hi Morten,

Thanks! I'm a VB.NET coder (and a new one as well) - can this code be
translated to VB?

Alex


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ray | last post: by
7 posts views Thread by 00_ChInkPoIntD12 | last post: by
3 posts views Thread by Ali Chambers | last post: by
3 posts views Thread by Ali Chambers | last post: by
2 posts views Thread by jediknight | last post: by
4 posts views Thread by rn5a | last post: by
2 posts views Thread by Randy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.