703035. Sorting Employees

Easy

Sorting Employees

Slug: sorting-employees5907

Difficulty: Easy

Id: 703035

Topic Tags: Arrays, Strings, Sorting, Data Structures, Algorithms

Company Tags: Oracle

Summary

The problem involves sorting a list of employees based on their names. The twist is that the names are given in the format "First Name Last Name" and we need to sort them alphabetically while considering both first and last names.

Detailed Explanation

To solve this problem, we can use the built-in sorting function available in most programming languages. However, since the problem statement mentions arrays and strings as topic tags, let's implement a custom sorting algorithm to learn more about how it works under the hood.

Here's a step-by-step breakdown of the solution:

  1. Split each employee name into two parts: first name and last name.
  2. Combine these two parts with a space in between to form the full name.
  3. Use the built-in sort function or implement a custom sorting algorithm (e.g., quicksort, mergesort) to sort the list of full names alphabetically.

For this problem, we can use a simple bubble sort algorithm since it's an easy problem and the arrays are relatively small.

Time complexity: O(n^2) Space complexity: O(1)

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.Arrays;
import java.util.Comparator;

public class EmployeeSort {
    public static String[] sortEmployees(String[] employees) {
        Arrays.sort(employees, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        });
        return employees;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.