0

How do I sort my product names in POS product list? Here's my code, which says names.sort() is not a function. In js file

for(var i = 0, len = this.product_list.length; i < len; i++)
{
    var product_node = this.render_product(this.product_list[i]);
    var names = this.product_list[i].display_name
    var sorted_names = names.sort();
    console.log('Sorted Names',sorted_names)
}
  • check console.log(names). Is it really returning an array or not. – Lalit Dec 05 '17 at 10:15
  • No, it displays me an error as: - "Uncaught TypeError: names.sort is not a function" –  Dec 05 '17 at 10:16
  • 2
    names is not an array try console.log(names) and see the result. That's why you are getting names.sort is not a function – Lalit Dec 05 '17 at 10:18

1 Answers1

1

Do something like this,

var names=[];//array for names
   for(var i = 0, len = this.product_list.length; i < len; i++)
{
    var product_node = this.render_product(this.product_list[i]);
    names.push(this.product_list[i].display_name);//push name in the array
}
names.sort();//sort names
console.log(names);//sorted array
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37