7

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.

3
  • Why not pass .sort() a small function that just returns personA.compareTo(personB)?
    – Pointy
    Apr 3, 2018 at 12:39
  • How to do it? Can you show me the way? Apr 4, 2018 at 14:52
  • personalities.sort((personA, personB) => personA.compareTo(personB));
    – Pointy
    Apr 4, 2018 at 14:53

2 Answers 2

6

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.

4
  • Imagine a .js file servicePerson. I call this to list persons by name. The service call personDAO that goes to DB and populate an array with objects of type personModel.js (where is my function person). If I want to order de array in Service layer, have I call persons.sort(<and pass all compare logical logical here>)? It smells bad! This logical has to be in Model layer and be universal to all DAO, service, controller, etc. Apr 4, 2018 at 14:50
  • No, that's the point of what I wrote. You definitely shouldn't pass the sorting logic as an anonymous function to sort(). Instead, define it somewhere, e.g. as a static member function of your model class, and pass that function to sort(). Apr 4, 2018 at 14:54
  • Sorry, now I wrote the code and understood your point. But I don't understand why this works :) person.compareTo = function ... why it is adding a function on my object? I can refctoring my person function now and change toString to person.toString = function... ;) Apr 4, 2018 at 17:14
  • person.compareTo is a "static method" of your person class. Strictly speaking, it's a method of your person() function. What this means is that you don't invoke it through instances of person, e.g. personA.compareTo (you'll actually get an error if you try to do this). Instead it's a method of the class itself. The whole topic goes a lot deeper, I'd recommend reading up on OOP in JavaScript to get a clearer picture. MDN has pretty good materials: developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects Apr 4, 2018 at 19:03
0

Really thanks for all responses. Máté gave me the path for a subject that I had not really knowlegment in JS.

My final class is:

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;
}

And my compareTo method now is static:

person.compareTo = 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;
                   };

All this code is in PersonModel.js.

Now at my service/DAO I can do this:

var personalities = new Array(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"),
                              new person("Alicia", 152, "F"));

personalities.sort(person.compareTo);

And the result will be:

Alicia 152 years old woman
Ayrton Senna 58 years old man
Bartolomeu Simpson 36 years old man
Jean J. Michel 37 years old man
Jean J. Michel 36 years old man

Well done, closed case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.