can you help me with a doubt?
I'm trying an approach more didatical in JS, creatind a POJO/POCO for a model class.
I have a Person object:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
return this;
}
Now I can create some data:
var personalities = new Array(new person("Alicia", 152, "F"),
new person("Bartolomeu Simpson", 36, "M"),
new person("Ayrton Senna", 58, "M"),
new person("Jean J. Michel", 36, "M"),
new person("Jean J. Michel", 37, "M"));
Well, done it if I call sort method there is no action to order this data:
personalities.sort();
for(var i = 0; i < personalities.length; i++)
console.log(personalities[i].name);
Output is:
Alicia
Bartolomeu Simpson
Ayrton Senna
Jean J. Michel
Jean J. Michel
I created a toString() method:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.toString = function() {
return this.name + " " +
this.age + " years old" +
(this.gender == 'M' ? " man" : " woman");
};
return this;
}
Now the sort method is "ok", the output now is:
Alicia
Ayrton Senna
Bartolomeu Simpson
Jean J. Michel
Jean J. Michel
But I want do something different, order by name and age (oldest first). I implemented the method compareTo(obj):
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.toString = function() {
return this.name + " " +
this.age + " years old" +
(this.gender == 'M' ? " man" : " woman");
};
this.compareTo = function(otherPerson) {
var resultOfComparison = 0;
if(this.name > otherPerson.name) {
resultOfComparison = 1;
return resultOfComparison;
}
else if(this.name < otherPerson.name) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(this.age > otherPerson.age) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(this.age < otherPerson.age) {
resultOfComparison = 1;
return resultOfComparison;
}
return resultOfComparison;
};
return this;
}
The method is ok, look it:
var jean1 = new person("Jean J. Michel", 36, "M");
var jean2 = new person("Jean J. Michel", 37, "M");
console.log(jean1.compareTo(jean2)); //1
console.log(jean1.compareTo(jean1)); //0
console.log(jean2.compareTo(jean1)); //-1
Can I use this approach to sort my array? Something like this:
personalities.sort(person.compareTo);
I really do not want to do it in a service class that use my model:
personalities.sort(function(personA, personB) {
var resultOfComparison = 0;
if(personA.name > personB.name) {
resultOfComparison = 1;
return resultOfComparison;
}
else if(personA.name < personB.name) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age > personB.age) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age < personB.age) {
resultOfComparison = 1;
return resultOfComparison;
}
return resultOfComparison;
});
I think that this is a model responsibility.
Thanks for all help.
Well, the comparator function you pass to sort()
needs to take two arguments, so it can't be an instance method. I'd suggest you define it as a "static" method:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
person.compare = function(personA, personB) {
if(personA.name > personB.name) return 1;
if(personA.name < personB.name) return -1;
return personB.age - personA.age;
}
And then you can call it when you sort:
personalities.sort(person.compare);
I hope I understood correctly what you were asking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With