Notifications
Clear all

[Closed] How to sort array of struct elements?

I need a quick help on this one can’t figure out…

If I have this for example:
struct person (name, height, age, sex)

Then I have a array MyGroup that contain X persons how can I sort that
array by Name or by height or age or sex?

And in optimal possibilities sort by Age + Name.

Thanks
OZRay

2 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

qsort is your friend.
i don’t have max around so i will try to make a code blind:

fn sortbyage v1 v2 = 
(
    if v1.age < v2.age then -1 else  if v1.age > v2.age then 1 else 0
)
fn sortbyname v1 v2 = 
(
    act = stricmp v1.name v2.name
    if act == 0 then sortbyage v1 v2 else act
)
qsort <an_array_of_persons> sortbyname 

Haaaa Ok thanks Denis! You align me on the right path.

Thanks!

OZRay