Link to home
Start Free TrialLog in
Avatar of nthawkins1971
nthawkins1971Flag for Hong Kong

asked on

datagrid column sorting incorrect

Hi, on the datagrid I have using AS3 when the column is selected as a sort critria it gets this incorrect based onb its values it goes -39, -4, -41, -47, -5, -55... etc please see the screen shot attached for a clearer picture of whats going on... is it because it onyl sorts by the first character? anyway I hope someone can help? many thanks
column-sort.jpg
ASKER CERTIFIED SOLUTION
Avatar of blue-genie
blue-genie
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nthawkins1971

ASKER

perfect thanks... slight change to the code and works fine

var AHDiscount:DataGridColumn = new DataGridColumn("AHDiscount");
AHDiscount.sortOptions = Array.NUMERIC;

Open in new window

oh gawd sorry dodgy copy and paste there.
no prob... quick one, how come if I add a percentage sign it creates the same problem...

AHDiscount:Math.round(AHTradesXML.tbl_DATA_AHShareNWAX[j].xDiscount*100) + "%"

Open in new window

hey as soon as you add your "%" you're converting it to a string again.
you will need to create a custom sort function.
(i'm assuming your percentage values are whole numbers ie. 5%, not 5.2%);

try this

AHDiscount.sortCompareFunction = sortPercentage;


function sortPercentage(itemA:Object, itemB:Object):int {
	if (toNumber(itemA.AHDiscount) > toNumber(itemB.AHDiscount)) {
		return -1;
	} else if (toNumber(itemA.AHDiscount) < toNumber(itemB.AHDiscount)) {
		return 1;
	} else {
		return 0;
	}
}

function toNumber(element:String):Number{
	
	 return Number(element.split("%").join(""));
}

Open in new window

Thanks a lot blue-genie, works a treat!