Skip to content

Instantly share code, notes, and snippets.

@tobie
Created May 24, 2013 12:41
Show Gist options
  • Save tobie/5643236 to your computer and use it in GitHub Desktop.
Save tobie/5643236 to your computer and use it in GitHub Desktop.
Looking for a good name for that sort of pattern, and more specifically how to name the `sortBy` function to best convey what it does.
function sortBy(prop) {
return function(a, b) {
return a[prop].localeCompare(b[prop]);
}
}
[
{ foo: "some text", bar: "other text" },
{ foo: "hello", bar: "world" },
{ foo: "More text", bar: "MOAR" }
].sort(sortBy('foo'));
@betehess
Copy link

It's just a higher-order function. Being a closure would imply some context to be captured, but here you're passing it to the function so it's not happening.

About naming the function, I believe that sortBy conveys the right information and would not change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment