# Help me implement a sorting method

**URL:** <https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982>\
**Category:** New to Julia\
**Tags:** sort\
**Created:** [April 10, 2019, 12:31am UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982 "2019-04-10T00:31:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 10, 2019, 12:31am UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/1 "2019-04-10T00:31:50Z")

</div>

I have something like

```julia
mutable struct Foo
    Data::Array{String, 1}
    Results:Array{Result,1}
end

```

Where

```julia
mutable struct Result
    Query::String
    Score::Float64
end

```

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.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 10, 2019, 1:14am UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/2 "2019-04-10T01:14:50Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 10, 2019, 6:09am UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/3 "2019-04-10T06:09:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 10, 2019, 1:11pm UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/4 "2019-04-10T13:11:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 10, 2019, 1:11pm UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/5 "2019-04-10T13:11:49Z")

</div>

Ops sorry, formatting fail. Fixed now.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 10, 2019, 4:31pm UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/6 "2019-04-10T16:31:14Z")

</div>

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.

```julia
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.

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 10, 2019, 6:30pm UTC](https://discourse.julialang.org/t/help-me-implement-a-sorting-method/22982/7 "2019-04-10T18:30:09Z")

</div>

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 🙂
