Tuesday, June 7, 2011

How To Sort ListItems In JavaScript

First Declare ListItemArray Varaible:-

var myOptions = [];

//Fill ListItem Array With Some Data

// copy options into an array
function SortListItems(optionText,optionValue) //where optionText and optionValue are array of texts and values
{ myOptions.clear();//clear array if any data present

// copy options into an array
for (var i=0; i
{ myOptions[i] ={ optText:optionText[i], optValue:optionValue[i]}; }

// sort array myOptions.sort(SortFuncAsc);}

// sort functionfunction SortFuncAsc(record1, record2){ var value1 = record1.optText.toLowerCase(); var value2 = record2.optText.toLowerCase();; if (value1 > value2) return(1); if (value1 < value2) return(-1); return(0);
}

No comments:

Post a Comment