473,773 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2978
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(EventArg s 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(stri ngs, new SortClass());
Array.Reverse(s trings);

listBox1.Items. AddRange(string s);
}

public class SortClass : IComparer
{
public int Compare(object x, object y)
{
if(!(x is String) || !(y is String))
throw new InvalidCastExce ption("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.TryP arse(a, NumberStyles.Fl oat, CultureInfo.Inv ariantCulture, out d)
|| !double.TryPars e(b, NumberStyles.Fl oat, CultureInfo.Inv ariantCulture, 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**@alexchamb ers.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5910
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the ArrayList only has one element in it everything works ok. But as soon as I have more than one element, I get the following exception when the control loads up: "Cannot modify the Items collection when the DataSource property is set.". Anybody...
0
2304
by: Ray | last post by:
Folks, I have just created a simple procedure that does the following: Determines the width of the columns of a listbox. Places a button of the correct size above each column as the form opens. Sorts by that column when you click the button. Automatically toggles between ASC and DESC sort sequence. To implement it on a new form, you simply copy the buttons from my
2
1817
by: Josema | last post by:
Hi to all, I have a datagrid filled with string values, and when i click in a header column the datagrid is alphanumerical sorted , but i need sort some columns by numeric way... The datasource of this Datagrid is a DataView, that is a View of a DataTable (using the property of the datatable.CurrentView) Do you know how could i solve the sorting problem to have a numerical sort,
7
10068
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if there is already one. C.P.
3
1755
by: Ali Chambers | last post by:
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>
3
384
by: Ali Chambers | last post by:
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>
2
7234
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column header. The text columns are sorted correctly but the numerical columns seem not to get sorted properly. For example I get the following result when I try to sort a numerical
4
4964
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be listed first followed by the files.
2
1914
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially display only. I'm trying to sort these lists alphabetically A->Z. I have the Sort property set to True. When I run the app, the items in each box are sorted correctly. So far, so good. What I am confused by is that when a category is...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.