mutant icon indicating copy to clipboard operation
mutant copied to clipboard

how to sort an observable collection

Open ahdinosaur opened this issue 8 years ago • 1 comments

hey @mmckegg @mixmix,

i noticed in the patchbay /network page, when migrating to use mutant observables we stopped using our peerListSort function. so when browsing the page, the peers i want to see (the ones i'm connected to) are randomly scattered about.

i attempted to write a function to sort a collection observable like so:

function sort (obsCollection, comparator) {
  const result = MutantArray()
  obsCollection(collection => {
    const sorted = collection.slice().sort(comparator)
    result.set(sorted)
  })
  return result
}

but the problem is that this "flattens" the collection of nested observables into a collection of values, which would lead to changes in our render functions with h.

any advice on how to do this properly? and should this be something included in mutant?

chur!

ahdinosaur avatar Feb 06 '17 04:02 ahdinosaur

Hey @ahdinosaur! Yes, I think you are right that it should be a mutant transform. I think it should work like map but instead of changing the output, changes the order.

Here's how i would implement sorting in patchbay (without a special mutant transform):

https://github.com/ssbc/patchbay/commit/ef1aeb93da0bbf554b9b3e36fbd834e889ac5f38

mmckegg avatar Feb 06 '17 06:02 mmckegg