Chalarangelo/30-seconds-of-code

A function to sort by the fieldName

MoisesTR opened this issue · 2 comments

I want to add a function to sort an array by field Name and you can specify the order you want, if you dont supply the second parameter the order it's ascending by default.

example:

const array = [
  { id: 1, name: "Alucard", age: 24, university: "Luxemburgo" },
  { id: 2, name: "Victoria", age: 23, university: "Palleis" },
  { id: 3, name: "Teo", age: 20, university: "Michigan" },
];

array.sort(byFieldName("name"));
array.sort(byFieldName("university"));
array.sort(byFieldName("age"));

// the 3 statements return the array in ascending order by default, if you want to specify other ordern, you can supply the another param
const order = "desc";
byFieldName(fieldName, order);

I could add this if it would be a good addition?

This already exists as orderBy.