What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Question] ListViewControl - Numeric sortable Columns

[Question] ListViewControl - Numeric sortable Columns
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: [Question] ListViewControl - Numeric sortable Columns
some remarks on the actual code of markee:

* while(PlusWnd.LstView_GetItemText(ControlID,0,i)!=undefined) isn't going to work since GetItemText is always going to return a string, even for columns which doesn't exist (undefined isn't returned). Thus, the code will probably loop indefinitely. So better use a fixed column count or check upon an empty string.

* There are some issues with the code, especially when used on listviews containing other stuff than numbers. But also on listviews containing only numbers, the sort routine can be made better:

* In your multidimensional array, you take the columns in the first dimension and the items in the second. If you do it the other way around you don't need to concatenate columns, nor use a reg. exp. to strip things back down.
It is also wrong to assume that the rest of the columns need to be sorted too, since they can have any order; aka you don't know which column to sort before the other column.

So, IMHO, better add the sorteable column as the first column, followed by all the other columns, into an array for each element. The main array (dimension 1) simply consists of all these smaller column arrays with the sorteable listview column as first column. Then simply sort that main array and you're done.

* Instead of removing everything and then adding everything again, it will be much faster to change the listview items with LstView_SetItemText once the array is sorted. The number of items doesn't change anyways.

EDIT: oh, and:
//method of sorting array
function num(x,y){
    var number = parseInt( x) - parseInt( y);
    return number === 0 ? 0 : number < 0 ? -1 : 1
}

Thus something like this:
code:
//Get the text in the List View
var arr = new Array();
for (var i=0; i < pPlusWnd.LstView_GetCount(CONTROLID); i++) {
        arr[i] = new Array();
        arr[i][0] = pPlusWnd.LstView_GetItemText(CONTROLID, i, SORTCOL);
        for (var j=0; j < SORTCOL; j++)
                arr[i][j+1] = pPlusWnd.LstView_GetItemText(CONTROLID, i, j);
        for (var j=SORTCOL+1; j < TOTCOLS; j++)
                arr[i][j] = pPlusWnd.LstView_GetItemText(CONTROLID, i, j);
}

//Arrange the data in the correct order
arr = arr.sort(function(x, y) {
        number = parseInt(x) - parseInt(y);
        return number == 0 ? 0 : number < 0 ? -1 : 1
});

//Put text back into the List View
for (var i=0; i < pPlusWnd.LstView_GetCount(CONTROLID); i++) {
        pPlusWnd.LstView_SetItemText(CONTROLID, i, SORTCOL, arr[i][0])
        for (var j=0; j < SORTCOL; j++)
                pPlusWnd.LstView_SetItemText(CONTROLID, i, j, arr[i][j+1]);
        for (var j=SORTCOL+1; j < TOTCOLS; j++)
                pPlusWnd.LstView_SetItemText(CONTROLID, i, j, arr[i][j]);
}
Where TOTCOLS is the total number of columns in the listview.
And SORTCOL is the column index (base 0) to sort.

This can of course be optimized further depending on how many columns you have, etc

And if you want sort descending, then either:
- change "-1 : 1"  into  "1: -1"
- or change "parseInt( x) - parseInt( y)"  into  "parseInt( y) - parseInt( x)"
- or change "number < 0"  into  "number > 0"
                      (well, you catch my drift: make sure the returned sign is changed in one way or the other :p)
in the unnamed sort function.

;)

This post was edited on 08-12-2007 at 05:41 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
01-13-2007 07:22 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[Question] ListViewControl - Numeric sortable Columns - by tryxter on 01-11-2007 at 11:00 PM
RE: [Question] ListViewControl - Numeric sortable Columns - by CookieRevised on 01-12-2007 at 04:59 AM
RE: [Question] ListViewControl - Numeric sortable Columns - by tryxter on 01-12-2007 at 11:31 AM
RE: [Question] ListViewControl - Numeric sortable Columns - by Mnjul on 01-12-2007 at 04:41 PM
RE: [Question] ListViewControl - Numeric sortable Columns - by markee on 01-13-2007 at 06:09 AM
RE: [Question] ListViewControl - Numeric sortable Columns - by CookieRevised on 01-13-2007 at 07:22 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On