These are chat archives for ramda/ramda

29th
Sep 2016
Travis LaDuke
@laduke
Sep 29 2016 05:34
I want to sort an array of objects on multiple props. Is this stack overflow the current state of things? http://stackoverflow.com/questions/26323317/natural-sort-array-of-objects-multiple-columns-reverse-etc
Markus Pfundstein
@MarkusPfundstein
Sep 29 2016 08:12
This message was deleted
Barry G
@bgits
Sep 29 2016 15:59
does ramda have a deep object comparison like _.isEqual?
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 16:00
R.equals
Barry G
@bgits
Sep 29 2016 16:01
awesome thanks
@Bradcomp and for not equal can I do !R.equals?
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 16:06
Yeah, there's also ...
R.complement
Travis LaDuke
@laduke
Sep 29 2016 16:45
Any tips on multi column sort? :)
LeonineKing1199
@LeonineKing1199
Sep 29 2016 16:51
You mean like sorting multiple arrays at once?
Travis LaDuke
@laduke
Sep 29 2016 17:22
Like an array of objects with multiple props
sort by artist, then by album type of thing
LeonineKing1199
@LeonineKing1199
Sep 29 2016 17:25
The most obvious way is the use sortBy with a custom comparator that encapsulates your logic.
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 17:28
:point_up:
LeonineKing1199
@LeonineKing1199
Sep 29 2016 17:37
I bet you could abstract that kind of pattern out though. If you wanted to sort an array of objects by multiple properties. I definitely think you'd want to use path so nested props would be supported. Maybe it call it, sortByProps and stick it in the cookbook.
sortByProps([String], array)?
Travis LaDuke
@laduke
Sep 29 2016 17:40
There's a big thread about it in one of the issues, but I couldn't tell if anything came of it. It links to the stackoverflow I linked yesterday. ^
LeonineKing1199
@LeonineKing1199
Sep 29 2016 17:40
It might be useful. But then again, it might be some over-abstracted bloat and it's just easier to have the consumer of the library define their own comparator
Travis LaDuke
@laduke
Sep 29 2016 17:41
I don't mind using some hardcoded comparator for now. I took a couple stabs at it but I'm not sure how it should look like.
Barry G
@bgits
Sep 29 2016 17:41
Is there something that can do a deep comparison between objects and return the difference?
LeonineKing1199
@LeonineKing1199
Sep 29 2016 17:41
Yeah, if it's easier to just hard-code it, it's easier to just hard-code it XD
Travis LaDuke
@laduke
Sep 29 2016 17:41
Just like OR some comparisons ?
Also, what I've done before is just run through the array twice, (sort by .album then by .artist) and that would work, but with ramda's sort, that doesn't seem to work.
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 17:45
Sorting in JavaScript isn't stable
R.comparator
Travis LaDuke
@laduke
Sep 29 2016 17:45
(not complaining)
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 17:45
Can be used to turn a boolean function into a sorting function
Travis LaDuke
@laduke
Sep 29 2016 17:45
I've got that far ;)
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 17:46
Sorry for just stating the obvious :-P
LeonineKing1199
@LeonineKing1199
Sep 29 2016 17:47
Huh, Ramda's docs don't mention stability
They probably should
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 17:52
@ram-bot
const objs = [{a: 1, b: 5}, {a: 0, b:6}, {a: 1, b: 4} ]

R.sort(R.comparator((x, y) => x.a === y.a ? x.b < y.b : x.a < y.a))(objs)
ram-bot
@ram-bot
Sep 29 2016 17:52
[ { a: 0, b: 6 }, { a: 1, b: 4 }, { a: 1, b: 5 } ]
Travis LaDuke
@laduke
Sep 29 2016 17:54
You're a champion.
Travis LaDuke
@laduke
Sep 29 2016 18:16
I need to do it on three properties and it's getting a little out of hand
Travis LaDuke
@laduke
Sep 29 2016 18:21
But it works. Nothing wrong with nested ternaries right?
Brad Compton (he/him)
@Bradcomp
Sep 29 2016 18:28
I dig 'em
Barry G
@bgits
Sep 29 2016 18:46
is there a to way to defined multiple lensProps to update, ie, I have const obj = {a: '1', b: '2', c: '3'}, const paths = lensPaths(['a', 'b']), set(toNumber, path, obj), output = '{a: 1, b: 2, c: '3'}` ?
LeonineKing1199
@LeonineKing1199
Sep 29 2016 22:33

@laduke

I'm not feeling too well today but to keep my mind busy, I took a stab at comparing up with a comparator generator. This is it:

'use strict';

const assert = require('assert');
const R = require('ramda');

const sort = R.sort;
const path = R.path;
const prop = R.prop;

const propComparator = 
  props => 
    (a, b) => {
      const length = props.length;
      for (let i = 0; i < length; ++i) {
        const propA = path(props[i], a);
        const propB = path(props[i], b);

        if (propA === propB) {
          continue;
        } else {
          return (propA < propB ? -1 : 1);
        }
      }
};


const props = [
  ['a'],
  ['b']
];

const data = [
  {
    a: 0,
    b: 2
  },
  {
    a: 1,
    b: 0
  },
  {
    a: 0,
    b: 1
  }
];


const sorted = sort(propComparator(props), data);

assert.deepEqual(
  sorted,
  [
    {
      a: 0,
      b: 1
    },
    {
      a: 0,
      b: 2
    },
    {
      a: 1,
      b: 0
    }
  ]);
Travis LaDuke
@laduke
Sep 29 2016 22:34
whoa