2

I'm confused why the console is showing me a sorted array at both logs. Because at the first point where I'm logging it shouldn't be sorted?

  static reloadAndSortItems() {
    let array = [];
    const items = Store.getStoredItems();

    items.forEach(function (item) {

      // getting the stored date --> back to date object

      let episodeDate = Date.parse(item.episode);
      let parsedEpisode = new Date(episodeDate);


      array.push(parsedEpisode);



    });

    **// should not sorted at this point
    console.log('not sorted', array);**



    let tested = array.sort(function (a, b) {
      return a - b
    });
    **// should be sorted array at this point
    console.log('sorted', tested);**





  }

this is the array that is coming in ( which is out of order) :

["2018-09-13T00:30:00.000Z","2018-09-14T05:25:00.000Z","2018-09-13T00:30:00.000Z","2018-09-11T01:30:00.000Z","2018-09-11T01:30:00.000Z"]

2 Answers 2

1

That's because sort() method mutates the initial array, and also returns a new one, so finally you will get two arrays with the same order of elements:

let arr = [1, 6, 2, 9, 3, 7];
let result = arr.sort((a, b) => a - b);

console.log('Original:', arr);
console.log('Final:', result);

To avoid such behavior, you can create additional array (for example, using map() method, it returns a new array and doesn't mutate the original one), and use it as your initial array:

let arr = [1, 6, 2, 9, 3, 7];
let duplicate = arr.map(d => d);
arr.sort((a, b) => a - b);

console.log('Sorted:', arr);
console.log('Duplicate of the initial array:', duplicate);

Sign up to request clarification or add additional context in comments.

Comments

1

The sort() method mutates the array it's called on, so the correct thing to do here is to log to console the array variable, rather than the tested variable:

static reloadAndSortItems() {
    let array = [];
    const items = Store.getStoredItems();

    items.forEach(function (item) {

      // getting the stored date --> back to date object

      let episodeDate = Date.parse(item.episode);
      let parsedEpisode = new Date(episodeDate);    

      array.push(parsedEpisode);    
    });

    array.sort(function (a, b) {
      return a - b
    });

    console.log('sorted', array);    
  }

Alternativly, you could clone the array variable by means of .map(), and then call the .sort() method on that cloned array, like so:

static reloadAndSortItems() {
    let array = [];
    const items = Store.getStoredItems();

    items.forEach(function (item) {

      // getting the stored date --> back to date object

      let episodeDate = Date.parse(item.episode);
      let parsedEpisode = new Date(episodeDate);    

      array.push(parsedEpisode);    
    });

    // Copy/clone the array using map into tested variable
    const tested = array.map(function(item) { return item; });

    // Sort the tested array. Calling sort on tested will leave array unaffected
    tested.sort(function (a, b) {
      return a - b
    });

    console.log('sorted', tested);  // Sorted
    console.log('array', array);    // Unsorted
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.