Need help to create Array sort function
Hi,
I have the following code snippet to do array sort, it is working but I would like to make it a function that can take an array and a sort field as parameters and return the sorted array.
- <script type="text/javascript">
var listitems = new Array();
listitems.push({id:1, firstName:'John', lastName : 'Doe'});
listitems.push({id:2, firstName:'Jane', lastName :'Doe'});
listitems.push({id:3, firstName:'Joe', lastName :'Smith'});
listitems.push({id:4, firstName:'Ann', lastName :'Smith'});
listitems.push({id:5, firstName:'Mike', lastName :'Smith'});
listitems.sort(function(a, b) {
return a.firstName.toUpperCase().localeCompare(b.firstName.toUpperCase());
})
</script>
How do I do it?
Thanks