Reputation: 3023
I have an object array called Persons and would like to sort by one of its members, I am new to javascript prorotype and not sure how to do this. The object looks like this:
[ Object { EntityId=0, Name="Edibert", Number="1234", Value=""}]
[ Object { EntityId=0, Name="Jairo", Number="1234", Value=""}]
So it has a few more items there for that array of object Persons. I know i can access the name by doing something like this.Persons[0].Name
. But how can i sort it by Name?.
thank you so much
Upvotes: 0
Views: 511
Reputation: 16020
You can do this without Prototype:
Persons.sort(function(a,b) {
if(a.Name < b.Name) { return -1; }
if(a.Name > b.Name) { return 1; }
return 0;
});
Use any properties of the object you want from within the sort
function, so long as you return one of the following values:
In reality, any negative or positive number would work as a return value, but -1
and 1
are conventional.
Upvotes: 1