lstrojny / functional-php

Primitives for functional programming in PHP
MIT License
1.98k stars 204 forks source link

Feature request: sortBy function #162

Closed gvlasov closed 5 years ago

gvlasov commented 6 years ago

Sorts an array by the value of a function of its elements:

sortBy(
  ["Bob","Jake","Ed","Murphy"],
  function($elem, $index, $collection) {
    return strlen($elem);
  }
);
// ["Ed", "Bob", "Jake", "Murphy"]

Analogous to:

sort(
    ["Bob","Jake","Ed","Murphy"],
    function($left, $right) {
        return strlen($left) - strlen($right);
    }
);
lstrojny commented 6 years ago

Sounds like a plausible use case, especially if one wants to sort a list of objects by e.g. a weight property. Would you you be interested in working on a PR?

lstrojny commented 5 years ago

Closing for inactivity