0

I have an array of objects that I want to sort by the title, then by whether another property has a value or not. I'm trying to d3.sort but I don't think I'm doing it correctly. I've tried:

filtered.sort(function(a, b) {
        d3.ascending(a.claimed, b.claimed) ||
            d3.ascending(a.title, b.title);

As well as a variation of this answer to better fit my needs, but neither of them seem to be sorting correctly. Is there something I am missing?

Here is a sample of the data in the object that I access I am working with:

author: "String"
claimed: "String" //This should be sorted first
mapClass: "String"
name: "String"
tags: "String"
thumbnail: "URL"
timestamp: "Date"
title: "String" //Then this should be second
url: "URL"
Community
  • 1
  • 1
BDD
  • 665
  • 17
  • 31
  • 2
    You're missing a return statement. :-) – gengkev Aug 28 '15 at 03:09
  • Also, I've never seen anyone use `||` in order to implement multiple sort keys. That's quite clever, although I usually have to write such code in Java, where that wouldn't work... – gengkev Aug 28 '15 at 03:13

1 Answers1

3

You're just missing a return statement! This seems to work for me.

filtered.sort(function(a, b) {
  return d3.ascending(a.claimed, b.claimed) ||
      d3.ascending(a.title, b.title);
});
gengkev
  • 1,890
  • 2
  • 20
  • 31
  • Gah! I can't believe I missed that. I guess that's what I get for staring at code for too long :) – BDD Aug 28 '15 at 03:28