Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Forked from dreadjr/example.js
Created June 8, 2016 05:50
Show Gist options
  • Save leocaseiro/c5280b7cc492da98cbc9c86d1c110fb9 to your computer and use it in GitHub Desktop.
Save leocaseiro/c5280b7cc492da98cbc9c86d1c110fb9 to your computer and use it in GitHub Desktop.

Revisions

  1. @dreadjr dreadjr revised this gist May 27, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    _.mixin({
    'orderKeysBy': function (obj, comparator, order) {
    if (_.isString(comparator) && _.isNull(order)) {
    if (_.isString(comparator) && _.isEmpty(order)) {
    order = comparator;
    comparator = null;
    }
    var keys = _.orderBy(_.keys(obj), function (key) {
    return comparator ? comparator(obj[key], key) : key;
    }, order);

    var values = _.map(keys, function (key) {
    return obj[key];
    });

    return _.zipObject(keys, values);
    }
    });
  2. @dreadjr dreadjr revised this gist May 27, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    _.mixin({
    'orderKeysBy': function (obj, comparator, order) {
    if (_.isString(comparator) && _.isNull(order)) {
    order = comparator;
    comparator = null;
    }
    var keys = _.orderBy(_.keys(obj), function (key) {
    return comparator ? comparator(obj[key], key) : key;
    }, order);
  3. @dreadjr dreadjr revised this gist May 27, 2016. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    _.mixin({
    'sortKeysBy': function (obj, comparator) {
    var keys = _.sortBy(_.keys(obj), function (key) {
    return comparator ? comparator(obj[key], key) : key;
    });
    'orderKeysBy': function (obj, comparator, order) {
    var keys = _.orderBy(_.keys(obj), function (key) {
    return comparator ? comparator(obj[key], key) : key;
    }, order);

    var values = _.map(keys, function (key) {
    return obj[key];
    });

    return _.zipObject(keys, _.map(keys, function (key) {
    return obj[key];
    }));
    }
    return _.zipObject(keys, values);
    }
    });
  4. @dreadjr dreadjr revised this gist May 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ _.mixin({
    return comparator ? comparator(obj[key], key) : key;
    });

    return _.object(keys, _.map(keys, function (key) {
    return _.zipObject(keys, _.map(keys, function (key) {
    return obj[key];
    }));
    }
  5. @colingourlay colingourlay revised this gist Sep 23, 2014. 2 changed files with 10 additions and 1 deletion.
    9 changes: 9 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    var obj = {b: 3, c: 2, a: 1};

    _.sortKeysBy(obj);
    // {a: 1, b: 3, c: 2}

    _.sortKeysBy(obj, function (value, key) {
    return value;
    });
    // {a: 1, c: 2, b: 3}
    2 changes: 1 addition & 1 deletion sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    _.mixin({
    'sortKeysBy': function (obj, comparator) {
    var keys = _.sortBy(_.keys(obj), function (key) {
    return comparator(obj[key], key);
    return comparator ? comparator(obj[key], key) : key;
    });

    return _.object(keys, _.map(keys, function (key) {
  6. @colingourlay colingourlay created this gist Sep 22, 2014.
    11 changes: 11 additions & 0 deletions sortKeysBy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    _.mixin({
    'sortKeysBy': function (obj, comparator) {
    var keys = _.sortBy(_.keys(obj), function (key) {
    return comparator(obj[key], key);
    });

    return _.object(keys, _.map(keys, function (key) {
    return obj[key];
    }));
    }
    });