kolkov / ngx-gallery

A simple responsive native gallery component for Angular 8+.
https://ngx-gallery.kolkov.ru/
MIT License
115 stars 56 forks source link

Images are not respecting array sort. #18

Closed aardvark79 closed 4 years ago

aardvark79 commented 4 years ago

If I sort the array of galleryImages, the new sort order is not reflected in the gallery.

Calling .sort on the array has no effect, but calling .splice works as expected.

How can I rearrange the images in the array and have it be reflected in the gallery at runtime?

I am actually using an array of a custom type that extends NgxGalleryImage and contains a property that I'm using to set the sort order. So it's actually more like:

// [images] input of <ngx-gallery/> tag points to this.property.galleryItems, 
// which extends NgxGalleryImage
// sortingArray contains an array of integers used to determine the new sort order
// of the property.galleryItems array

this.property.galleryItems.sort((a, b) => {
      let aIndex = this.sortingArray.indexOf(a.galleryItemID);
      let bIndex = this.sortingArray.indexOf(b.galleryItemID);

      if (aIndex < bIndex) {
        return -1;
      } else if (aIndex == bIndex) {
        return 0;
      }

      return 1;
});

And this does not rearrange the images in the gallery as I would expect it to.

aardvark79 commented 4 years ago

Nevermind - I added a slice() to create a new copy of the array and assigned it and it works.