function compareDirection (G, node, head = true) {
  return function (a, b) {
    var da = linkDirection(G, a, head)
    var db = linkDirection(G, b, head)
    var c = head ? 1 : -1

    // links between same node, sort on type
    if (a.v === b.v && a.w === b.w && Math.abs(da - db) < 1e-3) {
      if (typeof a.name === 'number' && typeof b.name === 'number') {
        return a.name - b.name
      } else if (typeof a.name === 'string' && typeof b.name === 'string') {
        return a.name.localeCompare(b.name)
      } else {
        return 0
      }
    }

    // loops to same slice based on y-position
    if (Math.abs(da - db) < 1e-3) {
      if (a.w === b.w) {
        return G.node(b.v).y - G.node(a.v).y
      } else if (a.v === b.v) {
        return G.node(b.w).y - G.node(a.w).y
      } else {
        return 0
      }
    }

    // otherwise sort by direction
    return c * (da - db)
  }
}
/**
 * Sort links based on their endpoints & type
 */
Sort links based on their endpoints & type
Sort links based on their endpoints & type
function compareDirection (G, node, head = true) {
  return function (a, b) {
    var da = linkDirection(G, a, head)
    var db = linkDirection(G, b, head)
    var c = head ? 1 : -1
    if (a.v === b.v && a.w === b.w && Math.abs(da - db) < 1e-3) {
      if (typeof a.name === 'number' && typeof b.name === 'number') {
        return a.name - b.name
      } else if (typeof a.name === 'string' && typeof b.name === 'string') {
        return a.name.localeCompare(b.name)
      } else {
        return 0
      }
    }
    if (Math.abs(da - db) < 1e-3) {
      if (a.w === b.w) {
        return G.node(b.v).y - G.node(a.v).y
      } else if (a.v === b.v) {
        return G.node(b.w).y - G.node(a.w).y
      } else {
        return 0
      }
    }
    return c * (da - db)
  }
}
function compareDirection ( G , node , head = true ) { return function ( a , b ) { var da = linkDirection ( G , a , head ) var db = linkDirection ( G , b , head ) var c = head ? 1 : - 1 if ( a . v === b . v && a . w === b . w && Math . abs ( da - db ) < 1e-3 ) { if ( typeof a . name === 'number' && typeof b . name === 'number' ) { return a . name - b . name } else if ( typeof a . name === 'string' && typeof b . name === 'string' ) { return a . name . localeCompare ( b . name ) } else { return 0 } } if ( Math . abs ( da - db ) < 1e-3 ) { if ( a . w === b . w ) { return G . node ( b . v ) . y - G . node ( a . v ) . y } else if ( a . v === b . v ) { return G . node ( b . w ) . y - G . node ( a . w ) . y } else { return 0 } } return c * ( da - db ) } }
Sort links based on their endpoints & type
Sort links based on their endpoints & type
// links between same node, sort on type // loops to same slice based on y-position // otherwise sort by direction