Thread overview
sort with char’s
Aug 10
Joel
Aug 12
Joel
August 10

An array of int’s works:

import std;

struct List(T) {
    class Node {
        T data;
        Node next;
        this(T data) {
            this.data=data;
        }
        override string toString() const {
            return data.to!string;
        }
    }
    Node start;
    this(T[] data...) {
        void add(T data) {
            auto newNode=new Node(data);
            if (start is null)
                start=newNode;
            else {
                auto cur=start;
                while(cur.next) {
                    cur=cur.next;
                }
                cur.next=newNode;
            }
        }
        data.each!(d => add(d));
    }
    int opApply(int delegate(ref T) dg) {
        auto cur=start;
        while(cur) {
            int result=dg(cur.data);
            if (result)
                return result;
            cur=cur.next;
        }
        return 0;
    }
    auto opIndex() {
      	return this.array;
    }
}

void main() {
    auto j=List!char("Joel".to!(char[]));
	j.each!(c => write(c, " "));
    writeln;
    j[].sort!"a>b".each!(c => c.writeln);
    auto i=List!int(1979,4,30);
    writeln;
    i[].sort!"a<b".each!(n => n.writeln);
}

I get this:

onlineapp.d(49): Error: none of the overloads of template `std.algorithm.sorting.sort` are callable using argument types `!("a>b")(char[])`
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/sorting.d(1925):        Candidate is: `sort(alias less = "a < b", SwapStrategy ss = SwapStrategy.unstable, Range)(Range r)`
  with `less = "a>b",
       ss = SwapStrategy.unstable,
       Range = char[]`
  must satisfy one of the following constraints:
`       hasSwappableElements!Range
       hasAssignableElements!Range
       ss != SwapStrategy.unstable`
August 10

On Thursday, 10 August 2023 at 01:43:32 UTC, Joel wrote:

>

An array of int’s works:

This is one of those cases where autodecoding gets in the way. You can disable it using byCodeUnit, like this:

j[].byCodeUnit.sort!"a>b".each!(c => c.writeln);
August 12

On Thursday, 10 August 2023 at 12:12:41 UTC, Rene Zwanenburg wrote:

>

On Thursday, 10 August 2023 at 01:43:32 UTC, Joel wrote:

>

An array of int’s works:

This is one of those cases where autodecoding gets in the way. You can disable it using byCodeUnit, like this:

j[].byCodeUnit.sort!"a>b".each!(c => c.writeln);

Using byCodeUnit I get extra letters with the following code.

import std;

struct List(T) {
    class Node {
        T data;
        Node next;
        this(T data) {
            this.data=data;
        }
        override string toString() const {
            return data.to!string;
        }
    }
    Node root;
    this(T[] data...) {
        void add(T data) {
            auto newNode=new Node(data);
            if (root is null)
                root=newNode;
            else {
                auto cur=root;
                while(cur.next) {
                    cur=cur.next;
                }
                cur.next=newNode;
            }
        }
        data.each!(d => add(d));
    }

    int opApply(scope int delegate(ref T) dg) {
        auto cur=root;
        while(cur) {
            int result=dg(cur.data);
            if (result)
                return result;
            cur=cur.next;
        }
        return 0;
    }

    auto opIndex() {
        static if (is(T==char))
            return this.array.byCodeUnit;
        else
            return this.array;
    }
}

void main() {
    auto sirname="christensen";
    auto name=List!char(sirname.to!(char[]));
    writeln(name[].each!(n => n.write)); // get "christensenyes" //, " <> ", name[].sort!"a>b".each!(n => n.write));
}
August 13

On Saturday, 12 August 2023 at 06:47:50 UTC, Joel wrote:

>
writeln(name[].each!(n => n.write)); // get "christensenyes" //, " <> ", name[].sort!"a>b".each!(n => n.write));

This writes each character individually, and then at the end writes the result of each with a newline.

In this case, each docs say:

Returns:
Yes.each if the entire range was iterated, No.each in case of early stopping.

Yes.each is going to print as "yes".

-Steve