1

I wrote a module that has sorting methods. I also tried to make it more flexible and destructure parameters to sort anything i will set creating object (more or less) but I don't know how to do it inside the sort method. What i tried is in the equal2 last method, but obviesly is not working fine. Does anyone have clue what should i correct in this example?

 let products =  [ { name: "Grejpfrut", calories: 170, color: "czerwony", sold: 8200 },
{ name: "Pomarańcza", calories: 160, color: "pomarańczowy", sold: 12101 },
{ name: "Cola", calories: 210, color: "karmelowy", sold: 25412 },
{ name: "Cola dietetyczna", calories: 0, color: "karmelowy", sold: 43922 },
{ name: "Cytryna", calories: 200, color: "bezbarwny", sold: 14983 },
{ name: "Malina", calories: 180, color: "różowy", sold: 8200 },
{ name: "Piwo korzenne", calories: 200, color: "karmelowy", sold: 9909 },
{ name: "Woda", calories: 0, color: "bezbarwny", sold: 62123 }
];

  class compareSold {

     constructor(param1, param2) {
         this.param1 = param1 ;
         this.param2 = param2 ;
         // this.products = products ;
         const { calories, sold } = products;
         this.sortType = {
             asc: (param1, param2) => {
                 // console.log(param1);
                 if (param1.sold > param2.sold) {
                     return 1
                 } else if (param1.sold === param2.sold) {
                     return 0
                 } else {
                     return -1
                 }
             },
             dsc: (param1, param2) => {
                 // console.log(param1);

                 if (param2.sold > param1.sold) {
                     return 1
                 } else if (param1.sold === param2.sold) {
                     return 0
                 } else {
                     return -1
                 }
             },
             eqal: (param1, param2) => {
                 // console.log(param1);

                 if (param1.calories > param2.calories) {
                     return 1
                 } else if (param1.calories === param2.calories) {
                     console.log('a wowowiwa !!!');

                 } else {
                     return -1
                 }
             },
             eqal2: (param1, param2) => {
                 console.log(param1);

                 if (param1.sold > param2.sold) {
                     return 1
                 } else if ({sold} === {sold}) {
                     console.log('a wowowiwa !!!');

                 } else {
                     return -1
                 }
             }
         }
     }

  }

  const { calories, sold } = products; const compareSoldASCPass = new
 compareSold();

 // function calories ({calories}) { console.log(calories)}; //


 // Type of sorting(asc, dsc) ////////////////////////////////////////
 const compareSoldASC =
 products.sort(compareSoldASCPass.sortType.eqal2)

 // console.log(compareSoldASC);


 module.exports =  compareSoldASC;
9
  • Can you fix your question please? Commented Jun 11, 2020 at 17:10
  • Fix how? this is my first post so I'm not sure what is wrong what is right. Commented Jun 11, 2020 at 17:20
  • What are compareSoldASCPass, compareSoldUp, compareSoldASC? Please provide a code example that we can run without errors to see the problem. Commented Jun 11, 2020 at 17:28
  • Ok I made corrections. Here it is explenation: const compareSoldASCPass is a simply new Instance of compare sold, const compareSoldASC is a method used on new instancein this spec situation is a method equal2 from compare sold class.... Code is working fine but my question is how to make destructure outside the class to pass specific arguments from products? Commented Jun 11, 2020 at 17:40
  • 1. const { calories, sold } = products; doesn't make any sense because products is an array 2. you don't need to pass anything to the constructor; the sort() method calls your functions and passes along the two parameters; you don't need them as instance variables and you don't need to destructure anything Commented Jun 11, 2020 at 17:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.