Skip to content

explore a slightly more generic solution #6

@michaelficarra

Description

@michaelficarra

Instead of providing just a specialised function that would be used like

array.sort(String.comparingByCodePoints);

it would be similarly ergonomic and more generally applicable to do something like

array.sort(Iterator.comparingBy(s => s.codePoints()));

assuming we had (roughly)

String.prototype.codePoints = function() {
  return this[Symbol.iterator]().map(c => c.codePointAt(0)));
};

and also (roughly)

Iterator.comparingBy = getIter => {
  const NIL = {};
  return (a, b) => {
    let as = getIter(a);
    let bs = getIter(b);
    for (let [a, b] of Iterator.zip([as, bs], { mode: "longest", padding: [NIL, NIL] )) {
      if (a === NIL) return -1;
      if (b === NIL) return 1;
      if (a < b) return -1;
      if (b < a) return 1;
    }
    return 0;
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions