Jump to content

Sort two listboxes based on one listbox


Docfxit
 Share

Recommended Posts

Hi,

I have a two list boxes. The first list box has a name. The second list box has a count. They go together. The first name belongs with the first count. I'd like to find out how to sort them by the count with the highest number at the top. This is my code with no sort:

Global $listtop = 80
Global $FontSize = 15
$nList1 = GUICtrlCreateList("", 10, $listtop, 185, 200, $WS_BORDER)
GUICtrlSetFont (-1,$FontSize, 400, 0, $font)
GUICtrlSetData(-1, $President[1]&"|"&$President[2]&"|"&$President[3]&"|"&$President[4]&"|"&$President[5])
$nList2 = GUICtrlCreateList("", 190, $listtop, 90, 200, $WS_BORDER)
GUICtrlSetFont (-1,$FontSize, 400, 0, $font)
GUICtrlSetData(-1, $PresidentCnt[1]&"|"&$PresidentCnt[2]&"|"&$PresidentCnt[3]&"|"&$PresidentCnt[4]&"|"&$PresidentCnt[5])

Thank you,

Docfxit

Link to comment
Share on other sites

I have seen you working on this....

I really think you should be using a "ListView"

Just click at the top of each column to sort or de-sort

#include <GuiConstants.au3>
#include <GuiListView.au3>

opt('MustDeclareVars', 1)
Dim $listview, $Btn_Exit, $msg, $Status, $Btn_Insert, $ret, $Input_Index
GUICreate("ListView Sort", 392, 322)

$listview = GUICtrlCreateListView("President's Name|Vote Count|Party", 40, 30, 310, 149)
GUICtrlSendMsg($listview, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($listview, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("Johnson|2225|Rep", $listview)
GUICtrlCreateListViewItem("Nixon|4225 |Dem", $listview)
GUICtrlCreateListViewItem("Washington|4110 |Dem", $listview)
GUICtrlCreateListViewItem("Carter|2993|Rep", $listview)
GUICtrlCreateListViewItem("Bush|3456 |Rep", $listview)
GUICtrlCreateListViewItem("Jackson|3559 |Dem", $listview)
GUICtrlCreateListViewItem("Van Buren|6557 |Rep", $listview)
GUICtrlCreateListViewItem("Madison|5621|Rep", $listview)
_GUICtrlListViewSetColumnWidth ($listview, 0, 75)
_GUICtrlListViewSetColumnWidth ($listview, 1, 75)
_GUICtrlListViewSetColumnWidth ($listview, 2, 75)
$Btn_Exit = GUICtrlCreateButton("Exit", 300, 260, 70, 30)

GUISetState()

Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($listview) ]

While 1
   $msg = GUIGetMsg()
   Select
      Case $msg = $GUI_EVENT_CLOSE Or $msg = $Btn_Exit
         ExitLoop
        Case $msg = $listview
            ; sort the list by the column header clicked on
            _GUICtrlListViewSort($listview, $B_DESCENDING, GUICtrlGetState($listview))
   EndSelect
WEnd

Exit

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

I have seen you working on this....

Yes I have. I have been working on different aspects of this script.

I really think you should be using a "ListView"

Thank you for sharing your thoughts. I'm not using ListView because I am using different colors in each column. Have you had any success with different background colors with ListView?

Just click at the top of each column to sort or de-sort

If I sorted the count column the name column wouldn't match. I tried to explain that in my original post. Each count belongs to one person. They must not get mixed up. This is displaying results of a vote. If Jack gets 500 votes Jacks name must be on the same row as Jacks votes.

Thanks for the input. I'm still looking for a solution.

Thank you,

Docfxit

Link to comment
Share on other sites

I found a couple solutions. I decided to sort the arrays before displaying them. This is the one I chose:

; Array Sort

#include<Array.au3>
Global $president[6][3] = [["6", "0", "0"],["0", "First", "99"],["0", "Third", "20"],["0", "Second","50"],["0", "Fifth","2"],["0", "Fourth", "3"]]
DisplayArray($president, "B4 Sort")
_Arraysort($President,1,1,0,UBound($President,2),2) 
DisplayArray($president, "After Sort")

Func DisplayArray($array, $Title, $ArrayElement=0)
    ConsoleWrite($Title & @LF)
    For $x = $ArrayElement To UBound($array,1) - 1
        ConsoleWrite($x & " - " & $array[$x][1] & " - " & $array[$x][2] & @LF)
    Next
EndFunc;==>DisplayArray

The results of the sort are:

B4 Sort

0 - 0 - 0

1 - First - 99

2 - Third - 20

3 - Second - 50

4 - Fifth - 2

5 - Fourth - 3

After Sort

0 - 0 - 0

1 - First - 99

2 - Second - 50

3 - Fourth - 3

4 - Third - 20

5 - Fifth - 2

My problem is it doesn't sort them in the proper order. Why would 3 come before 20? How could I fix that?

Thank you,

Docfxit

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...