Swift Coding Test Course, Sorting Digits in Descending Order

In this article, we will explore how to solve a specific problem using Swift. The topic is Sorting Digits in Descending Order. This problem allows us to practice string manipulation, sorting algorithms, and handling data types in Swift.

Problem Description

Given an array of integers, write a function that converts each integer to a string, sorts the strings in descending order by their digits, and returns the result. For example, if the array is [321, 123, 456], it should convert each integer to a string resulting in ["321", "123", "456"] and sort it in descending order, returning ["321", "456", "123"].

Problem Examples

  • Input: [321, 123, 456] → Output: ["321", "456", "123"]
  • Input: [12, 345, 7, 89] → Output: ["89", "7", "345", "12"]
  • Input: [10, 2, 1] → Output: ["2", "10", "1"]

Problem Approach

The process to solve this problem can be divided into the following steps:

  1. Convert the integers to strings.
  2. Sort the strings in descending order by their digits.
  3. Return the sorted result.

Step 1: Convert Integer Array to String Array

To convert an integer to a string, you can use Swift’s String() initializer. This allows you to convert all integers in the array to strings. This conversion can be easily performed using the map() function.

Step 2: Sort Strings in Descending Order

When sorting strings, you need to provide a custom sorting logic using the sorted(by:) method. Here, you can compare the lengths of the strings, or in case of equal lengths, compare them lexicographically.

Step 3: Return the Sorted Result

After sorting, you simply return the resulting array of strings. The final implementation will look like this.

Swift Code Implementation

func sortDigitsDescending(_ numbers: [Int]) -> [String] {
        // Step 1: Convert integers to strings
        let stringNumbers = numbers.map { String($0) }
        
        // Step 2: Sort strings in descending order (by digits)
        let sortedNumbers = stringNumbers.sorted { 
            (lhs, rhs) in
            if lhs.count != rhs.count {
                return lhs.count > rhs.count // Compare number of digits
            } else {
                return lhs > rhs // Compare values if lengths are equal
            }
        }
        
        // Step 3: Return the sorted result
        return sortedNumbers
    }

Code Explanation

The Swift function above works as follows:

  • numbers.map { String($0) }: Converts each element of the given integer array to a string.
  • sorted { ... }: Applies custom sorting logic to sort in descending order. It first compares the number of digits, and if they are the same, it compares the string values.
  • Returns the sorted array of strings as the result.

Test Cases

You can run a few test cases to check if the function works correctly. Below are some example test codes using the function.

let testCase1 = [321, 123, 456]
let result1 = sortDigitsDescending(testCase1)
print(result1) // ["321", "456", "123"]

let testCase2 = [12, 345, 7, 89]
let result2 = sortDigitsDescending(testCase2)
print(result2) // ["89", "7", "345", "12"]

let testCase3 = [10, 2, 1]
let result3 = sortDigitsDescending(testCase3)
print(result3) // ["2", "10", "1"]

Conclusion

In this article, we covered the problem of converting integers to strings and sorting them in descending order by digits using Swift. This was a useful exercise to enhance understanding of string manipulation and sorting algorithms. Problems like this frequently appear in coding tests and can be beneficial in real-world applications.

The coding implementation process allows you to utilize various features of Swift and aims to help improve problem-solving skills. In the next course, we will tackle even more interesting problems!