2

I have a structure for every user and I'm trying to sort them by users Lastname
Full code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct phNum
{
    char name[30];
    char lastName[30];
    int phone;
};

typedef struct phNum PhNum;

PhNum phList[5] = {};
void showData() {
    for(int i = 0;i < 5;i++) {
        if(strcmp(phList[i].name,"empty") == 0 && phList[i].phone == 0){
            printf("%d : %s | %s | null\n", i+1,phList[i].name,phList[i].lastName);
        }
        else {
            printf("%d : %s | %s | %d\n", i+1,phList[i].name,phList[i].lastName,phList[i].phone);
        }
    }
}

The array maximum is 5 items and the minimum is 0
I want to sort them ascending by their Lastname and if there are 4 items don't show the fifth one. and I want to override my Array phList and rewrite the sorted one my showData function is supposed to sort and print I tried These answers but I didn't understand: How to sort an array of structs in C? and How do I sort an array of structs in C by name, age and id?

I also tried to make a bubble sort algorithm but I was struggling to put my sorted data into my phList

Thanks For Help

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
BGOPC
  • 201
  • 9
  • Even for such a small array I would recommend [the standard `qsort` function](https://en.cppreference.com/w/c/algorithm/qsort). – Some programmer dude Jan 16 '23 at 13:57
  • I don't know that much about c I appreciate it if u write the code – BGOPC Jan 16 '23 at 13:58
  • Begin by writing a function that takes two arguments, of type "pointer to `struct PhNum`", and return an integer. The function should return a negative number if the first argument is "less" than the second, zero if both are equal, or a positive value if the first argument is "greater" than the second argument. Fortunately there already exists a standard function that return exactly that when comparing strings: `strcmp`. So call `strcmp` to compare the `lastName` of the two structures, and return the result. Now you have a function you can basically plug in directly for the `qsort` function. – Some programmer dude Jan 16 '23 at 14:04
  • There are also ***many*** examples, all over the Internet on how to use `qsort`, to sort any kind of arrays. Including arrays of structures. Experiment, and try something for yourself. That's a good way to learn. – Some programmer dude Jan 16 '23 at 14:05
  • [Here's something to start with](https://godbolt.org/z/633EKE1h7) – Ted Lyngmo Jan 16 '23 at 14:15
  • 1
    First start by writing a code example that uses `qsort` and sorts a plain int array. – Lundin Jan 16 '23 at 14:29

1 Answers1

2

For starters this declaration

PhNum phList[5] = {};

is invalid in C. Opposite to C++ in C you may not specify empty braces.

You could define the array like for example

PhNum phList[5] = { 0 };

To sort the array you can use standard C function qsort declared in header <stdlib.h>.

You need yourself to track how many actually initialized elements in the array. You may not change the array size.

For example if the array contains n initialized elements where n is greater than 0 and less than or equal to 5 then you can write

int cmp( const void *a, const void *b )
{
    const PhNum *p1 = a;
    const PhNum *p2 = b;

    return strcmp( p1->lastName, p2->lastName );
}    

and in main you can write

qsort( phList, n, sizeof( *phList ), cmp );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • [`{}` will be standardized in C23](https://www.iso-9899.info/n3047.html#6.7.10). (Assuming it gets approved in 2023...) – Andrew Henle Jan 16 '23 at 14:40
  • Thanks, but what is n?? – BGOPC Jan 16 '23 at 14:52
  • @BGOPC If you want to sort the whole array all elements of which are initialized with valid values then n is 5. Otherwise n is equal to the number of actually initialized elements of the array. If for example you set only 3 elements of the array and the last 2 are uninitialized then there is no sense to sort the whole array that is all its 5 elements. In this case you will need to set n equal to 3 to sort actually initialized elements that contain the initialized data member lastName – Vlad from Moscow Jan 16 '23 at 14:56
  • @VladfromMoscow Okay I understand, I make my code so if there is nothing in my list, There'll be some structures with default value not just an empty list so my n is always 5 – BGOPC Jan 16 '23 at 15:29