Ansistring: to sort numbers in a listbox in ascending or descending fashion

2
take
Last edited on
I'm assuming you mean the AnsiString used in C++ Builder.
See "Sorting the List" http://www.functionx.com/cppbuilder/controls/listbox.htm
Lots of material on C++ Builder here: http://www.functionx.com/cppbuilder/
Read the above and tried the following but it still does not work:

listBox2.Items.Add(listBox1.Items[j],Value);

}
listBox2.Sorted = true;

please help...thanks
1
2
    Form1->ListBox2->Items =  Form1->ListBox1->Items;
    Form1->ListBox2->Sorted = true;


I think the built-in TListBox sort will do ascending sequence only. If you need descending sequence, then you will need to do a bit more work.

However, "but it still does not work" is a bit vague. Could you please give a more precise description of the problem you are experiencing.

Last edited on
1
Last edited on
I'm sorry, I still don't know what errors you are actually getting. I'm not familiar with the .vsf file type.
I really intend to use Toint function to sort the list.

then you could replace this code:
1
2
    Form1->ListBox2->Items =  Form1->ListBox1->Items;
    Form1->ListBox2->Sorted = true;

with something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    for (int i=0; i<Form1->ListBox1->Items->Count; i++)
    {
        int n;
        // If the string does not contain a valid value,
        // an EConvertError exception is thrown.

        try {
             n = Form1->ListBox1->Items->Strings[i].ToInt();
        }
        catch (...)
        {
            continue;
        }
        AnsiString str;
        str.sprintf("%d", n);
        Form1->ListBox2->Items->Add(str);
    }
    Form1->ListBox2->Sorted = true;


Still, I'd like to see more of your code in order to be able to help properly. Please post the code which is giving you problems, and/or the full error messages you have.



ansi
Last edited on
I've looked at and tried to run the code above but it failed to compile for me at this line, ListBox1->Items->Add(k); because variable k is not defined.

Jennifer2525 wrote:
now I needed to copy over the numbers as text generated in listbox1 and display in listbox2 as integers and then sort them using the ToInt function

There is a problem with what you are attempting to do, "display in listbox2 as integers" in that the Listbox can only work with strings, so it cannot handle integers directly, the integer value must first be converted to a string.

It seems an unnecessary step, but it is possible to convert from int to string and then back to int if you like.

Perhaps I'm missing something here. Are you intending to use a separate array of integers and sort that array separately, before putting the values into the listbox in order to display the reults?

Maybe you have the original question or description of the project and perhaps would like to share that, as I'm a bit lost as to what it is you actually need to do here.
Topic archived. No new replies allowed.