1

I have an array of js object

const name = 'Levi'
var data = [ 
{ firstname: 'Levi', food: 'apple' },
{ firstname: 'Eren', food: 'orange' },
{ firstname: 'Levi', food: 'grapes' },
{ firstname: 'Miks', food: 'banana' }
];

How can I sort the firstname and prioritized (Levi) as on top.

Expected Output:

[{firstname: Levi, food: apple},
{ firstname: Levi, food: grapes },
{ firstname: Eren, food: orange },
{ firstname: Miks, food: banana}]

I used this code

this.data.sort((a, b) => (a.firstname > name) ? 1 : ((name> a.firstname) ? -1 : 0));
wentjun
42.9k10 gold badges108 silver badges116 bronze badges
asked Jan 31, 2020 at 3:42
2
  • Step 1: implement sorting you want but without prioritisation. When you have it - update the question. Commented Jan 31, 2020 at 3:45
  • this.data.sort((a, b) => (a.firstname === name) ? -1 : (b.firstname === name) ? 1 : a.firstname.localeCompare(b.firstname)); Commented Jan 31, 2020 at 3:47

2 Answers 2

1

The first check both a and b firstname are name or not, if true, do nothing.

Next, check a firstname or b firstname is name, if true, find who has firstname is name, and swap a and b or not.

Finally, check with alphabetical order rule.

const name = 'Levi'
var data = [
 { firstname: 'Levi', food: 'apple' },
 { firstname: 'Eren', food: 'orange' },
 { firstname: 'Levi', food: 'grapes' },
 { firstname: 'Miks', food: 'banana' },
 { firstname: 'AMiks', food: 'mango' }
];
const sorted = data.sort((a, b) => {
 if (a.firstname === name && b.firstname === name) {
 return 0;
 }
 if (a.firstname === name || b.firstname === name) {
 return a.firstname === name ? -1 : 1;
 }
 return a.firstname.localeCompare(b.firstname);
});
console.log(sorted);

Output:

[ { firstname: 'Levi', food: 'apple' },
 { firstname: 'Levi', food: 'grapes' },
 { firstname: 'AMiks', food: 'banana' },
 { firstname: 'Eren', food: 'orange' },
 { firstname: 'Miks', food: 'banana' } ]
answered Jan 31, 2020 at 4:05
Sign up to request clarification or add additional context in comments.

Comments

0

If a and b firstnames are equal to name, then sort on their food. If a firstname is name, then prioritize.

const name = "Levi";
var data = [
 { firstname: "Levi", food: "apple" },
 { firstname: "Eren", food: "orange" },
 { firstname: "Levi", food: "grapes" },
 { firstname: "Miks", food: "banana" }
];
const sortAsc = (a, b) => {
 if (b > a) {
 return -1;
 }
 if (a > b) {
 return 1;
 }
 return 0;
};
data.sort((a, b) => {
 if (a.firstname === name && b.firstname === name) {
 return sortAsc(a.food, b.food);;
 }
 if (a.firstname === name) {
 return -1;
 }
});
console.log(data);

answered Jan 31, 2020 at 4:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.