Search code examples
coding-stylenaming-conventionsconventionsconvention

method naming convention for sortBy orderBy with multiple arguments, best practices


For example i have table (e.g. in-memory collection) with name lastname fields and more.
Example JSON:

[
  {name: 'arina', lastname: 'anyone'},
  {name: 'bob',   lastname: 'zorro'},
  {name: 'bob',   lastname: 'black'},
]

now i want a data sorting method, that sorts the data by name and lastname

that means i want the result:

arina - anyone
bob   - black
bob   - zorro

but if i write something like

function sortByNameAndLastname(data) {
  //here i should first sort by lastname, then by name to get result i want
  sortByLastName(data);
  sortByName(data);
}

that is somehow confusing, if you read the order of calls.

What is the naming convention / best practices to name such method?

sortByNameAndLastname or sortByLastnameAndName ?

if you see SQL, there is a first case ORDER BY name, lastname returns the result i want.


Solution

  • You could be consistent with SQL syntax and use the naming convention of:

    OrderBy<firstField>Then<SecondField>

    For example in your case:

    OrderByNameThenLastName

    Your "name" field really should be renamed "firstname", especially as you have a field called "lastname" for the surname.