Need Help Please

I was just wondering, i have 2 listboxes, one which contains names, another with contains results, when ordering the listbox containing the names in alphabetical order, how do i order the listbox containing the results according to the names

Please



Answer this question

Need Help Please

  • Dana Reed

    There are several approaches to this, depending on what you want to do.

    If you simply want to display the sorted data, then abandon the scheme of using Listboxes and opt for using a DataGridView. DataGridView displays sorted data depending on the header you click.

    Try creating a datatable like (C#)

    System.Data.DataTable thetable = new DataTable("TABLE");

    thetable.Columns.Add(new Column("NAME",Type.GetType("System.String")
    ));

    thetable.Columns.Add(new Column("RESULT",Type.GetType("System.Int32")
    ));

     

    and then assuming you have a DataGridView named mygrid on your form, bind it to the datatable you have created by: (C#)

    mygrid.DataSource = thetable;

    this should do the trick.

    You can retrieve the sorted data by creating a DataRow array and then asking it to be filled with the Select method of the DataTable class like this: (C#)

    DataRow []therows = thetable.Select("","NAME DESC");

    or

    DataRow []therows = thetable.Select("","RESULTS DESC");

    this way the DataRow[] contains the rows sorted by the order you desire. On the plus side of this approach is that you can filter the rows you select by filling the first parameter of the Select method.

     

    Another approach is to try using the SortedList class in System.Collections namespace.

    Create a new SortedList then add the data to it in pairs like

    SortedList thelist = new SortedList();

    for (int i = 0; i < listbox1.Items.Count; i++)

    {

        thelist.Add(listbox1.Items[].ToString, listbox2.Items[].ToString);

    }

     

    the trick in this approach is to have the first item of the pair you add to the list as the one you want to sort by. When the data are inserted, they are sorted automatically so all you have to do is to take them out of the sortedlist and re-display them in listboxes or whatever you want to do.

    I hope anything of the above makes sense for you or at least that I pointed you to the right direction.

     


  • loopyloo

    Now pascal is something I have not used in quite a while.

    My humble guess on sorting would be that you have an array of records (if I remember correctly, structs there are called records), and then you sort the array by using a simple or more complex pascal algorithm.

    Say you have (please pardon my syntax, I am sure that I have it all wrong but the logic is the same nevertheless)

    type

    PAIR=record

    name: string;

    result: integer;

    end;

     

    var

    LIST: array[1..10] of PAIR;

     

    procedure sortthelist;

    var

        i,j: integer;

        tmp: PAIR;

    begin;

        for i:=1 to 10 do

            for j:=1 to 10 do

                if LIST[ i ].name > LIST[ j ].name then

                begin

                    tmp := LST[ i ]; LIST[ i ] := LIST[ j ]; LIST[ j ] := tmp;

                end;

    end;

     

    Now certainly this is not by far the most effective sorting algorith, basically it is the slowest there is, but it does the trick pretty nice and I am sure you can elaborate from here on...


  • Krighton

    Also,

    with the respective help above

    how would i go about resolving this problem with a pascal algorithm

    thank you


  • Need Help Please