Sort array based on presence of fields in objects JavaScript

In JavaScript, you can sort arrays of objects based on the presence of specific fields using a custom comparator function. This technique is useful when you need to prioritize objects with complete information.

Problem Statement

Let's say we have an array of people objects where some have both firstName and lastName, some have only one of these properties, and others have neither:

const people = [{
    firstName: 'Ram',
    id: 301
}, {
    firstName: 'Shyam',
    lastName: 'Singh',
    id: 1016
}, {
    firstName: 'Dinesh',
    lastName: 'Lamba',
    id: 231
}, {
    id: 341
}, {
    firstName: 'Karan',
    lastName: 'Malhotra',
    id: 441
}, {
    id: 8881
}, {
    firstName: 'Vivek',
    id: 301
}];

console.log("Original array:");
console.log(people);
Original array:
[
  { firstName: 'Ram', id: 301 },
  { firstName: 'Shyam', lastName: 'Singh', id: 1016 },
  { firstName: 'Dinesh', lastName: 'Lamba', id: 231 },
  { id: 341 },
  { firstName: 'Karan', lastName: 'Malhotra', id: 441 },
  { id: 8881 },
  { firstName: 'Vivek', id: 301 }
]

Sorting by Field Presence

We need to sort this array so that objects with both firstName and lastName appear first, then objects with only one of these properties, and finally objects with neither:

const people = [{
    firstName: 'Ram',
    id: 301
}, {
    firstName: 'Shyam',
    lastName: 'Singh',
    id: 1016
}, {
    firstName: 'Dinesh',
    lastName: 'Lamba',
    id: 231
}, {
    id: 341
}, {
    firstName: 'Karan',
    lastName: 'Malhotra',
    id: 441
}, {
    id: 8881
}, {
    firstName: 'Vivek',
    id: 301
}];

const sortByFieldPresence = (a, b) => {
    const aHasBoth = a.firstName && a.lastName;
    const bHasBoth = b.firstName && b.lastName;
    const aHasEither = a.firstName || a.lastName;
    const bHasEither = b.firstName || b.lastName;
    
    // Both have firstName and lastName
    if (aHasBoth && bHasBoth) return 0;
    if (aHasBoth) return -1;
    if (bHasBoth) return 1;
    
    // One has either firstName or lastName
    if (aHasEither && bHasEither) return 0;
    if (aHasEither) return -1;
    if (bHasEither) return 1;
    
    return 0;
};

people.sort(sortByFieldPresence);
console.log("Sorted array:");
console.log(people);
Sorted array:
[
  { firstName: 'Shyam', lastName: 'Singh', id: 1016 },
  { firstName: 'Dinesh', lastName: 'Lamba', id: 231 },
  { firstName: 'Karan', lastName: 'Malhotra', id: 441 },
  { firstName: 'Ram', id: 301 },
  { firstName: 'Vivek', id: 301 },
  { id: 341 },
  { id: 8881 }
]

Alternative Approach Using Priority Scoring

You can also use a priority-based approach by assigning scores to different field combinations:

const people = [{
    firstName: 'Ram',
    id: 301
}, {
    firstName: 'Shyam',
    lastName: 'Singh',
    id: 1016
}, {
    id: 341
}, {
    firstName: 'Karan',
    lastName: 'Malhotra',
    id: 441
}];

const getPriority = (obj) => {
    if (obj.firstName && obj.lastName) return 1; // Highest priority
    if (obj.firstName || obj.lastName) return 2; // Medium priority
    return 3; // Lowest priority
};

people.sort((a, b) => getPriority(a) - getPriority(b));

console.log("Sorted with priority scoring:");
console.log(people);
Sorted with priority scoring:
[
  { firstName: 'Shyam', lastName: 'Singh', id: 1016 },
  { firstName: 'Karan', lastName: 'Malhotra', id: 441 },
  { firstName: 'Ram', id: 301 },
  { id: 341 }
]

Key Points

  • Custom comparator functions return negative, zero, or positive values to determine sort order
  • Check for field existence using logical operators (&&, ||)
  • Priority-based sorting provides cleaner and more maintainable code
  • The sort method modifies the original array

Conclusion

Sorting arrays by field presence is useful for organizing data with optional properties. Use custom comparator functions with logical operators to achieve the desired sort order based on your specific requirements.

Updated on: 2026-03-15T23:18:59+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements