www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20932] New: Need a stableStaticSort to complement staticSort

https://issues.dlang.org/show_bug.cgi?id=20932

          Issue ID: 20932
           Summary: Need a stableStaticSort to complement staticSort
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: andrej.mitrovich gmail.com

Consider this simple example:

-----
import std.meta;

void main ()
{
    struct S (size_t idx, string val)
    {
        enum index = idx;
        enum value = val;
    }

    enum Comp (T1, T2) = T1.index > T2.index;

    alias seq = AliasSeq!(S!(0, "a"), S!(0, "b"), S!(1, "c"), S!(1, "d"), S!(2,
"e"), S!(2, "f"));

    alias sorted = staticSort!(Comp, seq);
    pragma(msg, sorted);
}
-----

Outputs:

(S!(2u, "e"), S!(2u, "f"), S!(1u, "d"), S!(1u, "c"), S!(0u, "b"), S!(0u, "a"))

But this is an unstable sort. It would be great to have a stable version. It's
output would be:

(S!(2u, "e"), S!(2u, "f"), S!(1u, "c"), S!(1u, "d"), S!(0u, "a"), S!(0u, "b"))

--
Jun 15