Ordering elements in a dropdown based on a property of the object
I have an array containing objects. I fill a dropdown using the array. How can the elements in the dropdown be arranged based a property of the object say ID?
The array has 3 elements (read objects at indexes 0,1, and 2). I want to rearrange my dropdown based on a property of the object in the array, say ID (which is 3,1 and 2 respectively).
Currently even if I bind the dropdown with sorted array, it is a futile effort, as the dropdown is indexed based on the name of the property.
_data.push(objItem[i]);
// here the array is being populated with data after it has been populated, I'm filling a dropdown using
System.Utils.fillSelectData("$selectDDL", _data, "FieldValue", "FieldText");
//our framework 开发者_JAVA百科supports this code But every time the binding is being done based on FieldText..
Any insight would be highly obliged..
ordering objects by id example:
var employees=[]
employees[0]={id: 1, name:"George", age:32}
employees[1]={id: 4, name:"Edward", age:17}
employees[2]={id: 2, name:"Christine", age:58}
employees[3]={id: 3, name:"Sarah", age:62}
var sorted = employees.sort(function(a, b) {
return a.id-b.id
});
for(var i=0;i<sorted.length;i++) {
alert(sorted[i].id);
}
If fillSelectData
of your framework doesn't have an optional parameter for the sorting key, then you can (1) extend it with such, or (2) fill the select element maually, avoiding this method completely.
Could you post some codee? :)
SEE Example 2: http://www.w3schools.com/jsref/jsref_sort.asp
function sortNumber(a,b)
{
return a - b;
}
var n = ["10", "5", "40", "25", "100", "1"];
document.write(n.sort(sortNumber));
精彩评论