0

I have a list of structs:

typedef struct {
    uint8_t mac_addr[6];
    uint32_t signal;
    uint32_t freq;
    ...
}pseudo_entry;

I'm sorting this entrys when inserting something in the list.

Pseudo:

while(next)
  if(curr.mac_addr < next.mac_addr)
      if(curr.signal < next.signal)
      ...

That's very inflexible and I want an ordering I can give as startup parameter. Any good ideas?

I don't need whole code. Just ideas or maybe libs.

Thanks. :)

Nick
  • 41
  • 3
  • 1
    There is no linked list in your code. If you were to use regular array of structs, you could use `qsort` with different callback functions for different ordering. – user694733 May 29 '17 at 13:50
  • 2
    When you use `<` to compare two arrays, you compare *pointers* actually, pointers to the first element in the arrays. Even if you have two arrays with the same contents, they will never compare as equal. If you want to compare arrays you have to write a function that compares the *contents* of the arrays. – Some programmer dude May 29 '17 at 13:50
  • Yeah I know. ^^ That's why I wrote Pseudo Code: – Nick May 29 '17 at 14:00

3 Answers3

0

You can convert your numbers into strings ( char* ) and then concatenate them all together into one string. I think it might be easier to sort them because then you need only one if statement. However converting and concatenating might take you more time than using the three conditions that you just mentioned in your question so you should compare times to check if it really worth it.

RoiHatam
  • 876
  • 10
  • 19
0

You may be interested in Intrusive Lists and data oriented design (Array of structures vs Structure of Arrays) in general. Advantages include fewer cache misses and better separation of data and code.

You can take in a function pointer if you're using C or a lambda if you're using C++ that takes care of sorting your values and returning them.

MattMatt2000
  • 622
  • 6
  • 15
0

You need a comparator function, and then you can pass it to the qsort function:

int compt(void *f, void *s){
  return (f->signal) -(s->signal);
}

and then:

qsort(&curr,<yourarraysize>, sizeof(cur), &compt);
Riccardo Bonafede
  • 610
  • 1
  • 9
  • 17