Home  >  Article  >  Backend Development  >  How to Sort a 2D Array in C by Column Values?

How to Sort a 2D Array in C by Column Values?

Linda Hamilton
Linda HamiltonOriginal
2024-11-06 01:43:02292browse

How to Sort a 2D Array in C   by Column Values?

Sorting a 2D Array in C : Built-In Functions and Custom Implementations

Introduction

Sorting multi-dimensional arrays in C can be a complex task. This article explores the capabilities of built-in functions and provides a custom implementation for effectively sorting a 2D array based on column values.

Built-In Functions

C offers limited built-in functions for sorting multi-dimensional arrays. The std::qsort function allows for sorting fixed-size arrays of any type. However, it does not provide direct capabilities for sorting multi-column arrays.

Custom Implementation

For multi-column sorting, a custom implementation using a comparator function can be utilized. This approach requires adapting the built-in std::sort function, which operates on elements in ascending order by default.

The comparator function takes two arrays as inputs and compares them based on the desired column value. In your case, you want to sort the 2D array by comparing the first column values. Here's a C implementation:

<code class="cpp">int comparator(int const *lhs, int const *rhs) {
    return (lhs[0] < rhs[0]) ? -1
        : ((rhs[0] < lhs[0]) ? 1
        : (lhs[1] < rhs[1] ? -1
        : ((rhs[1] < lhs[1] ? 1 : 0))));
}

In this comparator, we cascade ternary statements to compare the first column values and then the second column values to break ties.

Usage

To sort the array using the comparator, you can call the std::sort function with the array and the comparator as arguments:

<code class="cpp">std::sort(std::begin(ar), std::end(ar), comparator);

Example

Here's an example that demonstrates the custom sorting implementation:

<code class="cpp">#include <iostream>
#include <algorithm>

int ar[10][2] = {
    {20, 11},
    {10, 20},
    {39, 14},
    {29, 15},
    {22, 23}
};

int main() {
    int comparator(int const *lhs, int const *rhs);

    // Sort the array
    std::sort(std::begin(ar), std::end(ar), comparator);

    // Display the sorted array
    for (int i = 0; i < 10; i++) {
        std::cout << ar[i][0] << " " << ar[i][1] << '\n';
    }

    return 0;
}</code>

Output:

10 20
20 11
22 23
29 15
39 14

Conclusion

While C lacks dedicated built-in functions for multi-column array sorting, the custom implementation using a comparator function provides an efficient and flexible solution. This approach allows you to specify the desired sorting criteria and customize the sorting behavior based on your specific requirements.

The above is the detailed content of How to Sort a 2D Array in C by Column Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn