3

hi I want to sort a native array by a date that I get back from mysql. I want to extend the native array, so I don't have to type the same method over and over again

how can I achieve that?

here's my sorting:

[...].sort((a, b) => {
    return new Date(b.timestamp) - new Date(a.timestamp);
});
SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
  • 1
    You can extend Array prototype. Look at this https://stackoverflow.com/questions/21945675/extending-array-prototype-in-javascript – Héctor May 24 '19 at 16:28
  • 2
    Why not just declare your sort function as an external function and just pass it every time you call sort? Like: `[...].sort(sortByDate)`. You should be careful about overwriting existing functions because other libraries or code you use might depend on it. – Khauri May 24 '19 at 16:29
  • 1
    `function sortByDate(a, b) { return ... } /* .. */ [...].sort(sortByDate)` – Andreas May 24 '19 at 16:29
  • if I run it I get this: Cannot read property 'timestamp' of undefined – SwiftiSwift May 24 '19 at 16:30
  • 2
    Sort by `timestamp` is oddly specific and it seems unnecessary to pollute the prototype with this. You could create a generic `compareFunction`. Even that seems like overhead. `[...].sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))` is readable for he next person reading the code than checking some other file to see what's going on – adiga May 24 '19 at 16:30
  • and what should I do then? – SwiftiSwift May 24 '19 at 16:32
  • Why not do the sorting in your MySQL query? – Jordan Running May 24 '19 at 16:37
  • Most people are of the opinion that extending the prototype of native objects is a [bad idea](https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice). If you're a beginner, I'd recommend not getting into that habit. @adiga I doubt something like `array.sort(byTimestamp)` would confuse anyone looking at it. And if you're using something like VSCode, hovering over it or alt clicking the function will show you its definition. I'm pretty sure there's no overhead difference either. – Khauri May 24 '19 at 16:44

1 Answers1

4

I'm not 100% convinced that this is really better than just passing an array to function… but you can wrap the sort in a function and put it on the array prototype. This has the convenience of just being able to call it with dateSort() but lack the flexibility of passing in a callback:

Array.prototype.dateSort = function(desc = true) {
    this.sort((a, b) => {
      if (!desc) [a, b] = [b,a]
      return new Date(b.timestamp) - new Date(a.timestamp);
    });
  }

// Prevent dateSort from showing up when iterating over object keys:
 Object.defineProperty(Array.prototype, 'dateSort',{
    enumerable: false
  });

let arr = [
  {timestamp: '1995-12-17T03:24:00'},
  {timestamp: '1995-12-17T01:24:00'},
  {timestamp: '1995-12-17T02:24:00'},
  {timestamp: '1995-12-17T00:24:00'}
]

arr.dateSort()
console.log(arr)

arr.dateSort(false)
console.log(arr)

for (let i in arr){
 // no datesort when enumerating
 console.log(i)
}
Mark
  • 90,562
  • 7
  • 108
  • 148