-3

I have a data like,

option = [
  {
    "field": "bac",
    "title": "BAC",
    "hidden": true
  },
  {
    "field": "vm",
    "title": "VM",
    "hidden": false
  },
  {
    "field": "cad",
    "title": "CAD",
    "hidden": true
  },
  {
    "field": "boom",
    "title": "BOOM",
    "hidden": true
  }
];

Now i have to sort based on title, can someone help me how do i do that?

I tried like,

_.each(option, function(e)){
   arr.push(e.title);
}

arr.sort();

now getting every object and arranging, is that anyway to get this done more precise..

Thanks,

Jaccs
  • 1,052
  • 3
  • 12
  • 33
  • Use sort with comparison callback function. option.sort(function(obj1, obj2){ return obj1.title.localeCompare(obj2.title)}); – Alex S. Jul 02 '18 at 16:45

1 Answers1

0
arr.sort(function(a, b){
    if(a.title < b.title) return -1;
    if(a.title < b.title) return 1;
    return 0;
});
Milan
  • 179
  • 1
  • 8