-2

I have the following data

  var data = [
   {
    "h_id"  : "31",
    "city": "hill",       
}, {
    "h_id": "4",
    "city": "Bevery Hills",     
}, {
    "h_id": "5",
    "city": "New York",    
}, {
    "h_id": "31",
    "city": "New York",   
}, {
    "h_id": "5",
    "city": "New York",   
}
];

I want data to be sort by "id" order as follow

  var data = [
   {
    "h_id"  : "31",
    "city": "hill",       
}, {
    "h_id": "31",
    "city": "Bevery Hills",     
}, {
    "h_id": "4",
    "city": "New York",    
}, {
    "h_id": "5",
    "city": "New York",   
}, {
    "h_id": "5",
    "city": "New York",   
}
];

how do i create a function to filter the id value based on above scenerio

selvakumar
  • 620
  • 1
  • 8
  • 30

4 Answers4

2

You can pass a custom function to JavaScript's sort method:

var data = [{
  "h_id": "31",
  "city": "hill",
}, {
  "h_id": "4",
  "city": "Bevery Hills",
}, {
  "h_id": "5",
  "city": "New York",
}, {
  "h_id": "31",
  "city": "New York",
}, {
  "h_id": "5",
  "city": "New York",
}];

var result = data.sort(function(a, b) {
  return a.h_id.localeCompare(b.h_id);
});

console.log(result);
31piy
  • 23,323
  • 6
  • 47
  • 67
  • 4
    no need for `parseInt`, he literally treats those numbers as strings and sorts them alphabetically. E.g. "31" before "4" before "5" – Aleksey Solovey Feb 22 '18 at 11:54
  • I think the OP doesn't know what sorting he wants, let alone the sorting by string or integer – Klevin Delimeta Feb 22 '18 at 11:56
  • 2
    (_you can imagine splitting those numbers per character and treating them as districts_ : `3.1` < `4` < `5`) – Aleksey Solovey Feb 22 '18 at 11:57
  • @AlekseySolovey -- thanks for pointing out. Updated the code! – 31piy Feb 22 '18 at 11:59
  • I want to filter the id like 31,31,4,5,5 not like acednding or decending , i want to filter like if first id is 31 i want all the data in 31 in further object , then next id 4 means i want all data in 4 goes on – selvakumar Feb 22 '18 at 12:07
  • it shows localeCompare is not a function @31piy – selvakumar Feb 22 '18 at 12:11
  • @selvakumar you are using an **outdated browser** then, not every function from ES6 is supported. Check it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#Browser_compatibility) – Aleksey Solovey Feb 22 '18 at 12:23
0

You just need to call .sort method on your array with a comparator function.

var data = [{
  "h_id": "31",
  "city": "hill",
}, {
  "h_id": "4",
  "city": "Bevery Hills",
}, {
  "h_id": "5",
  "city": "New York",
}, {
  "h_id": "31",
  "city": "New York",
}, {
  "h_id": "5",
  "city": "New York",
}];

var sortedData = data.sort((a,b)=> b.h_id - a.h_id);
console.log(sortedData);
void
  • 36,090
  • 8
  • 62
  • 107
0

The sort() method sorts the elements of an array in place and returns the array. Read More about it here

And this is the way you can get it done

var data = [
   {
    "h_id"  : "31",
    "city": "hill",       
}, {
    "h_id": "4",
    "city": "Bevery Hills",     
}, {
    "h_id": "5",
    "city": "New York",    
}, {
    "h_id": "31",
    "city": "New York",   
}, {
    "h_id": "5",
    "city": "New York",   
}
];
  data.sort(function (a, b) {
          return parseInt(b.h_id) - parseInt(a.h_id);
     });
     
     console.log(data)
Muhammad Usman
  • 10,039
  • 22
  • 39
  • I want to filter the id like 31,31,4,5,5 not like acednding or decending , i want to filter like if first id is 31 i want all the data in 31 in further object , then next id 4 means i want all data in 4 goes on – selvakumar Feb 22 '18 at 12:07
0

You want to sort by string (not number) therefore according to Array sort docs you should do the following;

var data = [
   {
    "h_id"  : "31",
    "city": "hill",       
}, {
    "h_id": "4",
    "city": "Bevery Hills",     
}, {
    "h_id": "5",
    "city": "New York",    
}, {
    "h_id": "31",
    "city": "New York",   
}, {
    "h_id": "5",
    "city": "New York",   
}
];

data.sort(function(a, b) {
  if (a.h_id < b.h_id) {
    return -1;
  }
  if (a.h_id > b.h_id) {
    return 1;
  }
  return 0;
});

console.log(data);
Laazo
  • 467
  • 8
  • 22