Skip to content

Instantly share code, notes, and snippets.

@medfreeman
Forked from colingourlay/example.js
Created May 15, 2017 23:19
Show Gist options
  • Save medfreeman/203b10bd9beef1d89a035b336bb038e4 to your computer and use it in GitHub Desktop.
Save medfreeman/203b10bd9beef1d89a035b336bb038e4 to your computer and use it in GitHub Desktop.

Revisions

  1. @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) {
  2. @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];
    }));
    }
    });