def sortHKLd(HKLd, ifreverse, ifdup, ifSS=False):
    """sort reflection list on d-spacing; can sort in either order

    :param HKLd: a list of [h,k,l,d,...];
    :param ifreverse: True for largest d first
    :param ifdup: True if duplicate d-spacings allowed
    :return: sorted reflection list
    """
    T = []
    N = 3
    if ifSS:
        N = 4
    for i, H in enumerate(HKLd):
        if ifdup:
            T.append((H[N], i))
        else:
            T.append(H[N])
    D = dict(zip(T, HKLd))
    T.sort()
    if ifreverse:
        T.reverse()
    X = []
    okey = ''
    for key in T:
        if key != okey:
            X.append(D[key])
        okey = key
    return X
