Skip to content

Commit 0fa9a22

Browse files
committed
Add docs for ::by()
1 parent 9aaa099 commit 0fa9a22

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,24 @@ Compare::loose(1, "1"); // uses ==
128128
Compare::strict(1, 1) // uses ===
129129
```
130130

131-
But seriously, upgrade to 7.
131+
But seriously, upgrade to 7 and use the spaceship operator instead.
132+
133+
### PHP doesn't have a shorthand for sorting by fields
134+
When you're comparing a list of objects, you usually want to compare the same field on them repeatedly. Usually that means writing a usort function like this:
135+
136+
```
137+
Sort::user($list, function (HighScore $a, HighScore $b) {
138+
return $a->getPoints() <=> $b->getPoints();
139+
});
140+
```
141+
142+
And that's with the PHP 7 shorthand operator helping. This library offers a slightly shorter, Scala inspired version where you can just specify the function to retrieve the data for both objects.
143+
144+
```
145+
Sort::user($list, function (HighScore $a) {
146+
return $a->getPoints();
147+
});
148+
```
132149

133150
### PHP doesn't have chained usort
134151

0 commit comments

Comments
 (0)