0

How can I sort array something like below if any of fields is missing?

So existing array for example is:

const users = [
    {
        id: 1, firstname: 'Jerry'
    }, {
        id: 2, firstname: 'Thomas', lastname: 'Geib'
    }, {
        id: 3
    }, {
        id: 4, lastname: 'Berg'
    }, {
        id: 5, firstname: 'Ass', lastname: 'Noob'
    }, {
        id: 6, lastname: 'Jopa'
    }
]

and the result should be sorted in this order:

  1. Object with firstname and lastname
  2. Object only with firstname or lastname
  3. Object without firstname and lastname

so that it would look like:

    const users = [
        {
            id: 2, firstname: 'Thomas', lastname: 'Geib'
        }, {
            id: 5, firstname: 'Ass', lastname: 'Noob'
        }, {
            id: 1, firstname: 'Jerry'
        }, {
            id: 4, lastname: 'Berg'
        }, {
            id: 6, lastname: 'Jopa'
        }, {
            id: 3
        }
    ]

I've tried this sorting but result is not that I needed

users.sort((a,b) => {
    if (a.firstname === b.firstname) {
        return 0
    }
    if (!a.firstname) {
        return 1
    }
    return -1
});
7
  • 3
    what are you sorting your object based off? Commented Jan 29, 2019 at 12:45
  • lastname i guess Commented Jan 29, 2019 at 12:46
  • I suppose that the least number of object fields value to be the last one in array Commented Jan 29, 2019 at 12:48
  • Sorry, I really didn't text that. I edited question Commented Jan 29, 2019 at 12:50
  • 2
    @hofshteyn your edit still doesn't clarify what should happen if 2 users have both firstname and lastname? Commented Jan 29, 2019 at 12:50

2 Answers 2

6

You could sort by the check if the property exists. At the end sort by id ascending.

const users = [{ id: 1, firstname: 'Jerry' }, { id: 2, firstname: 'Thomas', lastname: 'Geib' }, { id: 3 }, { id: 4, lastname: 'Berg' }, { id: 5, firstname: 'Ass', lastname: 'Noob' }, { id: 6, lastname: 'Jopa' }];

users.sort((a, b) => 
    ('firstname' in b && 'lastname' in b ) - ('firstname' in a && 'lastname' in a) ||
    ('firstname' in b) - ('firstname' in a) ||
    ('lastname' in b) - ('lastname' in a) ||
    a.id - b.id
);

console.log(users);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

2

Well, just count the number of properties each item has and sort by this number, descending:

var users = [
  {id: 1,firstname: 'Jerry'}, 
  {id: 2,firstname: 'Thomas', lastname: 'Geib'}, 
  {id: 3}, 
  {id: 4,lastname: 'Berg'}, 
  {id: 5,firstname: 'Ass',lastname: 'Noob'}, 
  {id: 6,lastname: 'Jopa'}
];
users.sort(function(a, b) {
  var aw = ('firstname' in a) + ('lastname' in a);
  var bw = ('firstname' in b) + ('lastname' in b);
  return bw - aw;
});
console.log(users);

Comments

Your Answer

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.