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 3 2979
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]
Hi Morten,
Thanks! I'm a VB.NET coder (and a new one as well) - can this code be
translated to VB?
Alex
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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,
|
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.
|
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>
| |
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>
|
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
|
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.
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |