Help me implement a sorting method

I have something like

mutable struct Foo
    Data::Array{String, 1}


mutable struct Result

I want to be able to sort the contents of Foo by score but if score is equal alphabetically by Data. What would be the best way to do this? I found isless which can be defined for my own types, but is there a “swap” or similar function too? Since changing positions needs to be done in both arrays.

Thanks for any input.

Edit: There was a formatting error cutting of the struct declarations.

Put it into a DataFrame and then sort based on the columns you want? I do this with panel data quite often.

sort(df,[:score, :data])

Probably some code was cut off when you posted, please edit.

Yes this might be a solution, making the type contain a dataframe instead of arrays. Is there any difference in efficiency between the two?

Ops sorry, formatting fail. Fixed now.

I don’t know exactly how to visualize your problem but if you can think of your observations as Data containing an identifier or key variable with associated (scalar) information Query and Score, then a DataFrame seems natural.

df = DataFrame(data=Data,query=Query,score=Score)
sort!(df,[:score, :data])

It is not necessary to do it this way of course. That’s the beauty of the language. You could roll your own using sortperm, for example.

1 Like

Ah sortperm looks like a great solution actually, giving the correct position. You’re probably right that df could work if I just redefine the structure as well.

Exactly something I was looking for in the docs without finding it :slight_smile: