0
    let journal = [..............];
        //need sort by key time
        function sortByStatus(){
          return    journal.sort(function(a, b) {
                return a.fly_status - b.fly_status;
            });
        }

        function sortByTime(){
            return    journal.sort(function(a, b) {
                console.log(  new Date( a.start_datetime).getTime()  );
                return new Date( a.start_datetime).getTime()
 - new Date(  b.start_datetime).getTime();
            });
        }

How can i do this logic:

journal.sortByStatus().sortByTime().....some other methods of array ?

I can added those methods only journal array not all array in my page ?

zloctb
  • 10,592
  • 8
  • 70
  • 89
  • Just like here: http://stackoverflow.com/questions/42513268/how-to-trigger-a-anynomous-function-with-parameters-javascript/42513309#42513309, just change *String* into *Array* and the logic ofc. – kind user Mar 26 '17 at 14:36
  • 2
    Don't do it. `journal` is an array, you should not add custom methods to it. – Bergi Mar 26 '17 at 14:37
  • @Bergi Should not what? Add new methods to the Array.prototype? – kind user Mar 26 '17 at 14:41
  • @Kinduser correct. One reason is that they might collide with functions added later to the Ecmascript standard or by frameworks you decide to use in the future – John Dvorak Mar 26 '17 at 14:51
  • @JanDvorak What about learning purposes? I have done it hundred times and as you can see, I'm still alive. – kind user Mar 26 '17 at 14:53
  • @Kinduser you can do all sorts of unclean stuff in learning projects, and run away before they have the time to bite you. BTW, Ruby recently added a keyword that does a similar thing, but only in the lexical scope defined by the keyword, so that your definitions wouldn't throw off other code. No such protection exists in Javascript. – John Dvorak Mar 26 '17 at 14:57

1 Answers1

1

One way to do it could be to define your own object. This example doesn't allow for chaining the methods but you could call them sequentially:

let Journal = function(list){
  this.list = list || [];
  
  this.sortByStatus = function(){
    return this.list.sort(function(a, b) {
      return a.fly_status - b.fly_status;
    });
  }

  this.sortByTime = function(){
    return this.list.sort(function(a, b) {
      return new Date( a.start_datetime).getTime() - new Date(  b.start_datetime).getTime();
    });
  }
}

let journal = new Journal([
  {fly_status:50, start_datetime: 5},
  {fly_status:5, start_datetime: 50}
]);

console.log(journal.sortByStatus());
console.log(journal.sortByTime());

To allow for chaining, return the object itself after the method is complete (although with sorting, you'd probably just pick one sort or the other):

let Journal = function(list){
  this.list = list || [];
  
  this.sortByStatus = function(){
    list.sort(function(a, b) {
      return a.fly_status - b.fly_status;
    });
    return this;
  }

  this.sortByTime = function(){
    list.sort(function(a, b) {
      return new Date( a.start_datetime).getTime() - new Date(  b.start_datetime).getTime();
    });
    return this;
  }
    
}

let journal = new Journal([
  {fly_status:50, start_datetime: 5},
  {fly_status:5, start_datetime: 50}
]);

console.log(journal.sortByStatus().sortByTime().list);
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61