1

I have data structure something like this.

[        {"name": "John", "gender": "Male", "id": "1"},        {"name": "Max", "gender": "Male", "id": "2"},        {"name": "John", "gender": "Male", "id": "3"},        {"name": "Sam", "gender": "Male", "id": "4"},        {"name": "Max", "gender": "Male", "id": "5"},        {"name": "Sam", "gender": "Male", "id": "6"},        {"name": "Sam", "gender": "Male", "id": "7"},        {"name": " ", "gender": "Male", "id": "8"},        {"name": "John", "gender": "Male", "id": "9"},        {"name": " ", "gender": "Male", "id": "10"},    ]

I am trying to sort on it based onname property.I need all the records withname=max should come first followed byname=Sam and thenname=John and the remaining records that have empty name field or any other value.

I tried different techniques but didn't get the desired results.

Can someone suggest to me how I can achieve this?

Thanks.

Giannis's user avatar
Giannis
1,8601 gold badge15 silver badges35 bronze badges
askedDec 24, 2020 at 7:16
brig's user avatar
1

4 Answers4

2

You can give scores to eachname case, and then use the score inArray.sort function.

const data = [  { name: 'John', gender: 'Male', id: '1' },  { name: 'Max', gender: 'Male', id: '2' },  { name: 'John', gender: 'Male', id: '3' },  { name: 'Sam', gender: 'Male', id: '4' },  { name: 'Max', gender: 'Male', id: '5' },  { name: 'Sam', gender: 'Male', id: '6' },  { name: 'Sam', gender: 'Male', id: '7' },  { name: ' ', gender: 'Male', id: '8' },  { name: 'John', gender: 'Male', id: '9' },  { name: ' ', gender: 'Male', id: '10' }];const getScore = name => {  if (name === 'Max') return 3;  else if (name === 'Sam') return 2;  else if (name === 'John') return 1;  else return 0;};data.sort((a, b) => getScore(b.name) - getScore(a.name));console.log(data);

answeredDec 24, 2020 at 7:30
michael's user avatar
Sign up to request clarification or add additional context in comments.

Comments

2

You can do the following,

data = [        {"name": "John", "gender": "Male", "id": "1"},        {"name": "Max", "gender": "Male", "id": "2"},        {"name": "John", "gender": "Male", "id": "3"},        {"name": "Sam", "gender": "Male", "id": "4"},        {"name": "Max", "gender": "Male", "id": "5"},        {"name": "Sam", "gender": "Male", "id": "6"},        {"name": "Sam", "gender": "Male", "id": "7"},        {"name": " ", "gender": "Male", "id": "8"},        {"name": "John", "gender": "Male", "id": "9"},        {"name": " ", "gender": "Male", "id": "10"},    ]data.sort((a, b) => {  if(a.name < b.name) {    return 1;  } else if(a.name > b.name) {    return -1;  }  return 0;})console.log(data);

answeredDec 24, 2020 at 7:22
Md Sabbir Alam's user avatar

1 Comment

Thanks, sabbir.alam for the answer. I even tried this solution but output that I am expecting is. Max, Sam, John. etc. Above code gives Sam, Max, John, etc.
0

What if your data contains case sensitive name ?

const getScore = name => {  if (name.toLowerCase() === 'max') return 3;  else if (name.toLowerCase() === 'sam') return 2;  else if (name.toLowerCase() === 'john') return 1;  else return 0;};data.sort((a, b) => getScore(b.name) - getScore(a.name));console.log(data);
svarog's user avatar
svarog
9,8855 gold badges66 silver badges79 bronze badges
answeredDec 24, 2020 at 9:11
Shivam Gupta's user avatar

Comments

-1

Hi you can use this code

var arr = [    {"name": "John", "gender": "Male", "id": "1"},    {"name": "Max", "gender": "Male", "id": "2"},    {"name": "John", "gender": "Male", "id": "3"},    {"name": "Sam", "gender": "Male", "id": "4"},    {"name": "Max", "gender": "Male", "id": "5"},    {"name": "Sam", "gender": "Male", "id": "6"},    {"name": "Sam", "gender": "Male", "id": "7"},    {"name": " ", "gender": "Male", "id": "8"},    {"name": "John", "gender": "Male", "id": "9"},    {"name": " ", "gender": "Male", "id": "10"},];var arr1 = arr.filter((value, index) => {    if(value.name == 'Max'){        return value    }});var arr2 = arr.filter((value, index) => {    if(value.name != 'Max'){        return value    }});arr2 = arr2.sort((a, b) => (a.name < b.name) ? 1 : -1);var output = arr1.concat(arr2);console.log(output);
answeredDec 24, 2020 at 7:56
Daud khan's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.