-2

what is different between these two block of code , the outcome of the result are the same. can any one help me and explain the difference?

  const sortPeople = people.sort((first, last) => {
  const [firstName, lastName] = first.split(",");
  first.lastName > last.lastName ? 1 : -1;
});
  console.log(sortPeople);

second code

  const alpha = people.sort((lastOne, nextOne) => {
  const [aLast, aFirst] = lastOne.split(', ');
  const [bLast, bFirst] = nextOne.split(', ');
  return aLast > bLast ? 1 : -1;
});
console.log(alpha);

sample code sandbox

Zoe
  • 27,060
  • 21
  • 118
  • 148
Fenici
  • 461
  • 1
  • 5
  • 19
  • 4
    *"What is different between these two filter?"* Neither of them is a filter. Both of them are wrong (if the goal is to sort correctly). – T.J. Crowder Apr 22 '19 at 11:43
  • Where did you get these? The first is a bit nonsensical (and both have issues), as it seems to be trying to sort an array containing both strings and objects with a `lastName` property. (You *could* have an array of objects that had a `split` method and a `lastName` property, but...) We need a **lot** more context and a clearer, more defined question than "what's different" in order to help you. – T.J. Crowder Apr 22 '19 at 11:44
  • the purpose is Sort the people alphabetically by last name, – Fenici Apr 22 '19 at 11:45
  • @T.J. Crowder perhaps you could try the sample code at the bottom ? in case you missed it – Fenici Apr 22 '19 at 11:46
  • The first - do nothing. The second - sorted by last name. – hoangdv Apr 22 '19 at 11:46
  • Related: [*Why doesn't my arrow function return a value?*](https://stackoverflow.com/questions/45754957/why-doesnt-my-arrow-function-return-a-value). – T.J. Crowder Apr 22 '19 at 11:47
  • @hoangdv how do i test it ? – Fenici Apr 22 '19 at 11:47
  • @Fenici - I don't have to, I can read the code, which clearly gets the return value wrong when the entries are **the same**. Also: Being snarky is not a useful way to get help, now or in the future. – T.J. Crowder Apr 22 '19 at 11:47
  • 1
    @hoangdv - Maybe, if the implementation of `sort` isn't too confused by the fact the callback gets the return value wrong for equal entries. :-) The second one's `return` should be `return aLast.localeCompare(bLast);` or at least `return aLast == bLast ? 0 : aLast > bLast ? 1 : -1;`. – T.J. Crowder Apr 22 '19 at 11:50
  • @T.J.Crowder sorry just new to js , ok i got it . so the sorting require first and next entry , and we have to specify each one of them , in order to get array sort it ? – Fenici Apr 22 '19 at 11:53
  • I'm afraid I have no idea what you're asking. Make sure the full content of your question is **in** your question (not just linked off-site), show inputs and ouputs (again, *in* the question), what result you want, what you're seeing instead, and what it is you don't understand. – T.J. Crowder Apr 22 '19 at 11:55
  • Related(?): [*How to sort an array of objects by multiple fields?*](https://stackoverflow.com/questions/6913512/how-to-sort-an-array-of-objects-by-multiple-fields) – T.J. Crowder Apr 22 '19 at 11:55

3 Answers3

0

You need to return the difference of two last names, like

people.sort((a, b) => a.split(', ')[0].localeCompare(b.split(', ')[0]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

They will never get the same results !

For the first code you will notice the following

const [firstName, lastName] = first.split(",");// will cause an error if it's an array of objects you are sorting

first.lastName > last.lastName ? 1 : -1; // will cause an error if it's an array of strings you are sorting

and you also should wrap it using a return statement if you will type code before the return, JavaScript will not add the implicit return in such a case

return(first.lastName > last.lastName ? 1 : -1);

People is an array of objects containing last names, thats why it may access first lastName and second lastName From "first" and "last" objects passed to the arrow function directly.

Array of people should be [{ lastName:"john"},{lastName:"brad"},....] the object may be also holding other properties such as firstName , age, whatever, in order to use it in that way.


While For the second code People is an array of strings ["last,first","doe,john"....]

const [aLast, aFirst] = lastOne.split(', '); const [bLast, bFirst] = nextOne.split(', ');

lastOne and nextOne passed to the arrow function seems to be strings contains both "last,first" separated by a comma, and after splitting you are destructuring the the array that will result from splitting "lastOne" and "nextOne" strings and use their lastName in sorting using 2 new variables that will hold values to check

Ahmed Safar
  • 186
  • 7
-1
     const sortPeople = people.sort((first, last) => {
  const [firstName, lastName] = first.split(",");
  first.lastName > last.lastName ? 1 : -1;
});
  console.log(sortPeople);

above part does nothing it always returns -1. because first and last or not object so you can't do first.lastName. But second code.

const alpha = people.sort((lastOne, nextOne) => {
  const [aLast, aFirst] = lastOne.split(', ');
  const [bLast, bFirst] = nextOne.split(', ');
  return aLast > bLast ? 1 : -1;
});
console.log(alpha);

does sort string based on their values before comma. but as array is already sorted so it always return -1. that's why both code of block has same result.

Ahmed Waqas
  • 245
  • 1
  • 3
  • 14