# Fast and simple search for an object by field value, like in a database

**URL:** https://discourse.julialang.org/t/fast-and-simple-search-for-an-object-by-field-value-like-in-a-database/54127
**Category:** New to Julia
**Created:** [January 28, 2021, 4:04pm UTC](https://discourse.julialang.org/t/fast-and-simple-search-for-an-object-by-field-value-like-in-a-database/54127 "2021-01-28T16:04:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [January 28, 2021, 4:04pm UTC](https://discourse.julialang.org/t/fast-and-simple-search-for-an-object-by-field-value-like-in-a-database/54127/1 "2021-01-28T16:04:09Z")

</div>

Hello, I need a fast and simple search for an object by field value, like in a database.

for example,  
search for an object with the maximum field value,  
search by object field value  
search object with next value up

What data structure can i use for this?

May be i should create a list or dict for searching fields?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [January 28, 2021, 4:07pm UTC](https://discourse.julialang.org/t/fast-and-simple-search-for-an-object-by-field-value-like-in-a-database/54127/2 "2021-01-28T16:07:08Z")

</div>

Put your objects in a array and use findfirst, findmax etc. If you need better than O(n), you’ll something else, such as a tree.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [January 28, 2021, 4:24pm UTC](https://discourse.julialang.org/t/fast-and-simple-search-for-an-object-by-field-value-like-in-a-database/54127/3 "2021-01-28T16:24:52Z")

</div>

Databases are fast because they index various columns then can use those index to find the correct row (object) in the database quickly. I haven’t really run into any package that maintains multiple indexes on a set of objects so you can quickly find the object you want based on some criteria.

You could probably create a custom object using the structures in:

> **[GitHub - JuliaCollections/DataStructures.jl: Julia implementation of Data...](https://github.com/JuliaCollections/DataStructures.jl)**
>
> Julia implementation of Data structures. Contribute to JuliaCollections/DataStructures.jl development by creating an account on GitHub.

Something like:

```julia
using DataStructures

struct Foo
    value1::Int64
    value2::Float64
end

struct Collection
    data::Vector{Foo}
    lookup1::SortedDict{Int64, Int}
    lookup2::SortedDict{Float64, Int}
end

function Base.push!(c::Collection, v::Foo)
    if haskey(c.lookup1, v.value1) || haskey(c.lookup2, v.value2)
        throw("Foo is not unique")
    end
    push!(c.data, v)
    c.lookup1[v.value1] = length(c.data)
    c.lookup2[v.value2] = length(c.data)
end

function highest_value1(c::Collection)
    return c.data[last(c.lookup1)]
end

function lowest_value1(c::Collection)
    return c.data[first(c.lookup1)]
end

```

You would have to take a look at the documentation of Datastructures.jl it gives you methods to iterate over the SortedDict objects in key order.
