def _key_complex_for_display(a):
    """
    Key function to sort complex numbers for display only.

    Real numbers (with a zero imaginary part) come before complex numbers,
    and are sorted. Complex numbers are sorted by their real part
    unless their real parts are quite close, in which case they are
    sorted by their imaginary part.

    EXAMPLES::

        sage: import sage.arith.misc
        sage: key_c = sage.arith.misc._key_complex_for_display

        sage: key_c(CC(5))
        (0, 5.00000000000000)
        sage: key_c(CC(5, 5))
        (1, 5.00000000, 5.00000000000000)

        sage: CIF200 = ComplexIntervalField(200)
        sage: key_c(CIF200(5))
        (0, 5)
        sage: key_c(CIF200(5, 5))
        (1, 5.00000000, 5)
    """
    ar = a.real()
    ai = a.imag()
    if not ai:
        return (0, ar)
    epsilon = 1e-10
    if ar.abs() < epsilon:
        ar_truncated = 0
    elif ar.prec() < 34:
        ar_truncated = ar
    else:
        ar_truncated = ar.n(digits=9)
    return (1, ar_truncated, ai)