View Full Version : sorting data in listbox
jwiatrowski
10-18-2004, 07:32 AM
OK, I thought this would just work but I really need some help as this has been stumping me for awhile.
I have a listbox populated as:
plistBox.addItem("CountyA", 96550);
plistBox.addItem("CountyB", 15792);
plistBox.addItem("CountyC", 899676);
plistBox.addItem("CountyD", 205334);
Where "CountyA" is a county in a state and the data is the population.
The plistBox displays the above fine.
Then, I want the user to be able to sort the list by alpha or population ascending or descending. I've implemented this via a comboBox with the following code:
countySortBox.setChangeHandler("countySortHandler");
function countySortHandler(component){
switch(countySortBox.getSelectedItem().label){
case "Sort Alpha Ascending":
plistBox.sortItemsBy("label", "ASC");
break;
case "Sort Alpha Descending":
plistBox.sortItemsBy("label", "DESC");
break;
case "Sort Number Ascending":
plistBox.sortItemsBy("data", "ASC");
break;
Case "Sort Number Descending":
plistBox.sortItemsBy("data", "DESC");
break;
}
}
Sorting the counties alphabetically works correctly but sorting by poulation does not - the problem. When clicking 'Sort Number Descending' the result is:
CountyA 96550
CountyC 899676
CountyD 205334
CountyB 15792
when it should be:
CountyC 899676
CountyD 205334
CountyA 96550
CountyB 15792
It seems as though it's sorting by the first digit in the population instead of the actual number. Can someone help me here? Is there a setting/primitive or something that I am overlooking? I'm still new
mmm..pi..3.14..
10-18-2004, 08:12 AM
Well just a guess and most likely it won't work, but since it sorts the county name correctly but not the population, try treating the population as a string and not a number. You could convert it back to a number if you needed to later. So try something like this:
plistBox.addItem("CountyA", "96550");
plistBox.addItem("CountyB", "15792");
plistBox.addItem("CountyC", "899676");
plistBox.addItem("CountyD", "205334");
Just shooting in the dark here :rolleyes:
Eric
jwiatrowski
10-19-2004, 04:19 AM
I've tried that 1, same result. Thanks though. This is really starting to annoy me.
I do have a custom symbol so that the population value displays - could that be an issue?
Jake
curryjon51
10-21-2004, 01:57 AM
Hi
I get an error:
Line 19: ';' expected
Case "Sort Number Descending":
It should be case, with a small c.
hth
jwiatrowski
10-22-2004, 12:56 AM
OK, thanks, but there's a small c in the actual program. I mis-typed when writing the post. Can you sort by the #?
Jake
curryjon51
10-22-2004, 04:15 PM
Hi
Don't know your set up, but took the code you posted and made this fla... it gave me the error with the capital C, and when I corrected that, it seems to sort stuff just fine.
Hth
jwiatrowski
10-23-2004, 06:15 AM
Hi,
I ran your fla and it did indeed sort fine. Hmmm, I don't think I explained this well. The problem does not show itself if the actual values of the numbers sort in the same fashion as the first digits would sort if they were the lone value. Example, I added a 2 to the 2nd listbox entry that you provided, so now the listbox holds:
plistBox.addItem("CountyA", 1111);
plistBox.addItem("CountyB", 22222);
plistBox.addItem("CountyC", 3333);
plistBox.addItem("CountyD", 4444);
Now, the 22222 is the largest value. If I sort by 'Sort Number Descending' the listbox displays:
County D
County C
County B
County A
when it should show:
County B
County D
County C
County A
I hope that makes sense
Thanks again,
Jake
curryjon51
10-23-2004, 11:40 AM
Hi
Ok... fed the numbers into it using a little array. Now it seems to sort on single,double and triple figures, which is strange, because in principle, this fla is no different to the last one, as far as I can see.
hth
jwiatrowski
10-23-2004, 06:11 PM
Hi,
Again I ran your fla and it sorted fine, and I was thinkin that feeding it with an array was the solution, but... Just to make sure it was sorting based on the actual value of the number I changed the second value from 410 to 110. Now, County B - 110 is the highest value and I have the same problem :confused:
I don't know what is going on here!!! Appreciate your help and I'm not trying to sabotage your files, I just want the darn listbox to sort by the data properly!
Jake
curryjon51
10-24-2004, 12:59 AM
Hi
Yes.. beats me that one.... can anyone else give jwiatrowski and myself a hand here please?
Tx
the code that does the sorting is in the DataProvider object.... thats where it would need to be tweeked.
curryjon51
10-24-2004, 02:22 PM
Hi
Tx tg... and mmm..pi..3.14.. also.The help files say thet the data for the listbox and the combo box should be an array of strings, and combining that with the data provider thing seems to do it.
nums = ["410", "420", "2", "3"];
ltrs = ["CountyA", "CountyB", "CountyC", "CountyD"];
dir = ["Sort Alpha Ascending", "Sort Alpha Descending", "Sort Number Ascending", "Sort Number Descending"];
Larr = new Array();
Sarr = new Array();
for (var i = 0; i<nums.length; i++) {
Lob = new Object();
Lob.data = _level0.nums[i];
Lob.label = _level0.ltrs[i];
Larr[i] = Lob;
Sob = new Object();
Sob.data = _level0.nums[i];
Sob.label = _level0.dir[i];
Sarr[i] = Sob;
}
_root.plistBox.setDataProvider(Larr);
countySortBox.setDataProvider(Sarr);
//
//::::::::::::::::::::::::::::::::
//
countySortBox.setChangeHandler("countySortHandler");
function countySortHandler(component) {
switch (countySortBox.getSelectedItem().label) {
case "Sort Alpha Ascending" :
plistBox.sortItemsBy("label", "ASC");
break;
case "Sort Alpha Descending" :
plistBox.sortItemsBy("label", "DESC");
break;
case "Sort Number Ascending" :
plistBox.sortItemsBy("data", "ASC");
break;
case "Sort Number Descending" :
plistBox.sortItemsBy("data", "DESC");
break;
}
}
jwiatrowski
10-25-2004, 06:32 AM
Yes, thank you tg, mmm..pi..3.14.. and curryjon51. Again I thought this would do the trick and I implemented the above. All seemed great then with eager anticipation I added "98" as a fifth number to the nums array and tried to sort... wouldn't you know the 98 appeared on top for the sort descending and bottom for the ascending. Now what? Man, I've tried tons of approaches to this... Any thoughts?
its the way arrays are sorted in flash... for some reason it flash treats number arrays as strings. try this code i found on proto[type]:
// author: cdMan [+], Submitted: 05.05.03 1a (http://proto.layer51.com/d.aspx?f=847)
Array.prototype.sortNum = function() {
var tmp = [1];
for (var i = 0; i<this.length; i++) {
for (var j = 0; j<tmp.length; j++) {
if (this[i]<=tmp[j]) {
tmp.splice(j, 0, this[i]);
break;
} else if (j == tmp.length-1) {
tmp.splice(j, 0, this[i]);
break;
}
}
}
tmp.pop();
return (this=tmp);
};
var ar=new Array(1,33,453,123456,9);
ar.sort();
trace(ar);//output: 1,123456,33,453,9
trace(ar.sortNum());// output correct : 1,9,33,453,123456
//to reverse order
trace(ar.sortNum().reverse());//output: 123456,453,33,9,1
use this to sort the .data, use what you've got to sort the .label
curryjon51
10-26-2004, 12:33 AM
... Thanks tg.
jwiatrowski
11-21-2004, 04:20 AM
thanks tg and curryjon51 for all your help! I apologize for the delay on my response - I haven't had time to look at my flash stuff for a couple of weeks. I'm going to give this a try right now.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.