Question: Is it possible to sort objects using the Thrust library?
I have the following struct:

```
struct OB{
  int N;
  Cls *C; //CLS is another struct.
}

```

Is it possible to use thrust in order to sort an array of OB according to N? Can you provide a simple example on using thrust to sort objects? If thrust is not able to do so, is there any other CUDA libraries that allows me to do so?

Answer: 
Even though you may sort the objects by using special struct definitions, using a struct as functor, it will make thrust to change the sort algorithm from radix-sort to merge-sort. Speed of radix-sort is noticeably faster than merge-sort. So when using thrust, try to use integer types as key values as possible.

I may suggest you using "thrust::sory\_by\_key(..)" function.

You should change your struct from AOS to SOA structure.

```
struct OB{
  int N;
  Cls *C; //CLS is another struct.
}

```

to

```
struct OBs{
   int []Ns; -> thrust::device_vector<int> indices;
   Cls *C[]; -> thrust::device_vector<Cls> values;
}

```

When you sort the indices with sort\_by\_key, the values will already be sorted.

```
thrust::sort_by_key(indices.begin(), indices.end(), values.begin());

```
Up until now you cannot sort custom objects. You can do key based sorting but not of custom objects like the struct you mentioned. There are a couple of other open CUDA based algorithms available for doing this but that too requires some modification etc to make them work for you.