question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sort ascending, by key

See original GitHub issue

This would be so useful to have in D3:

d3.ascendingKey = function(key) {
   return function (a, b) {
      return a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : a[key] >= b[key] ? 0 : NaN;
   };
}

Alternatively also supporting accessor functions, but I think that’s less often needed and possibly inefficient:

d3.ascendingKey = function(key) {
   return typeof key == 'function' ? function (a, b) {
          return key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : key(a) >= key(b) ? 0 : NaN;
      } : function (a, b) {
          return a[key] < b[key] ? -1 : a[key] > b[key] ? 1 : a[key] >= b[key] ? 0 : NaN;
      };
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mbostockcommented, Oct 27, 2014

A shorter but slightly less efficient version:

d3.ascendingKey = function(key) {
  return function(a, b) {
    return d3.ascending(a[key], b[key]);
  };
};

In practice I tend to do things like:

array.sort(function(a, b) { return d3.ascending(a.foo, b.foo); });
0reactions
mbostockcommented, Oct 21, 2015

I’m not planning on adding these to 3.x, but hopefully 4.0’s module system will make it easier to bundle plugins like d3-jetpack.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python List sort() - Programiz
The sort() method sorts the items of a list in ascending or descending order. ... sort by name (Ascending order) employees.sort(key=get_name).
Read more >
ASCENDING KEY and DESCENDING KEY phrases - IBM
ASCENDING KEY and DESCENDING KEY phrases ... Data is arranged in ascending or descending order, depending on the keyword specified, according to the...
Read more >
PHP ksort() Function - W3Schools
The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative...
Read more >
Python sorting dictionaries: Key [Ascending] and then Value ...
I have looked for ages and have found no actual answers because I can't see any that start with an ascending key and...
Read more >
Python | Sort Python Dictionaries by Key or Value
First, sort the keys alphabetically using key_value.iterkeys() function. · Second, sort the keys alphabetically using the sorted (key_value) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found