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 )
in the unnamed sort function.