# How to sort a GPU StructArray vector by a subfield and update location of other elements in place?

**URL:** <https://discourse.julialang.org/t/how-to-sort-a-gpu-structarray-vector-by-a-subfield-and-update-location-of-other-elements-in-place/113617>\
**Category:** GPU\
**Created:** [April 29, 2024, 4:18pm UTC](https://discourse.julialang.org/t/how-to-sort-a-gpu-structarray-vector-by-a-subfield-and-update-location-of-other-elements-in-place/113617 "2024-04-29T16:18:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 29, 2024, 4:18pm UTC](https://discourse.julialang.org/t/how-to-sort-a-gpu-structarray-vector-by-a-subfield-and-update-location-of-other-elements-in-place/113617/1 "2024-04-29T16:18:26Z")

</div>

Hi! I have the following code:

```julia
using Random
using StaticArrays
using StructArrays
using CUDA

# Function to generate a simplified SimParticles with random values
function generate_simplified_particles(num_particles::Int)
    positions = [SVector{3, Float64}(randn(3)) for _ in 1:num_particles]
    densities = [rand() for _ in 1:num_particles]
    types = [1, 2, 3][rand(1:3, num_particles)] # Randomly assign one of these three types
    
    # Create a StructArray from the generated fields
    particles = StructArray((Position = positions, Density = densities, Type = types))
    
    return particles
end

# Generate 10 random particles
simplified_particles = generate_simplified_particles(10)

# Display the generated particles
for p in simplified_particles
    println("Position: $(p.Position), Density: $(p.Density), Type: $(p.Type)")
end

sort!(simplified_particles, by = p -> p.Type)

CUDA.allowscalar(true)
    simplified_particles_GPU = replace_storage(CuVector, simplified_particles)
CUDA.allowscalar(false)

# How do I properly sort the GPU struct array, similar to what I did for the CPU version?

```

I want to sort the GPU StructArray in-place similar to what I do for the CPU structarray. Is there a way to do what I wish?

Kind regards
