Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:38:16+00:00 2026-06-17T12:38:16+00:00

I am currently sorting values by key the following way thrust::sort_by_key(thrust::device_ptr<int>(keys), thrust::device_ptr<int>(keys + numKeys),

  • 0

I am currently sorting values by key the following way

thrust::sort_by_key(thrust::device_ptr<int>(keys), 
                    thrust::device_ptr<int>(keys + numKeys),
                    thrust::device_ptr<int>(values);

which sorts the “values” array according to “keys”.

Is there a way to leave the the “values” array untouched and instead store the result of sorting “values” in a separate array?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:38:17+00:00Added an answer on June 17, 2026 at 12:38 pm

    There isn’t a direct way to do what you are asking. You have two options to functionally achieve the same thing.

    The first is make a copy of the values array before the call, leaving you with a sorted and unsorted version of the original data. So your example becomes

    thrust::device_vector<int> values_sorted(thrust::device_ptr<int>(values),
                                         thrust::device_ptr<int>(values + numKeys));
    
    thrust::sort_by_key(thrust::device_ptr<int>(keys), 
                        thrust::device_ptr<int>(keys + numKeys),
                        values_sorted.begin());
    

    The second alternative is not to pass the values array to the sort at all. Thrust has a very useful permutation iterator which allows for seamless permuted access to an array without modifying the order in which that array is stored (so an iterator based gather operation, if you will). To do this, create an index vector and sort that by key instead, then instantiate a permutation iterator with that sorted index, something like

    typedef thrust::device_vector<int>::iterator iit;
    
    thrust::device_vector<int> index(thrust::make_counting_iterator(int(0)),
                                     thrust::make_counting_iterator(int(numKeys));    
    
    thrust::sort_by_key(thrust::device_ptr<int>(keys), 
                        thrust::device_ptr<int>(keys + numKeys),
                        index.begin());
    
    
    thrust::permutation_iterator<iit,iit> perm(thrust::device_ptr<int>(values),
                                               index.begin());
    

    Now perm will return values in the keys sorted order held by index without ever changing the order of the original data.

    [standard disclaimer: all code written in browser, never compiled or tested. Use at own risk]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have 2 ways of displaying images in a cell, which way will
Currently I have 3 values that I need to sort, in the following order:
I'm looking for a way to maintain the sorting on my key-value pairs. They
I am currently creating a sorting method that consists of values from an mysql
I need to use natural sorting with a MySQL result since it currently sorts
Before I sort I can access array values by key correctly. var a=[]; a['1']={'id':'1','aaa':'xxx'}
Currently when sorting in a flex grid, the sort arrow that shows whether the
Possible Duplicate: Sorting a hash in Ruby by its value first then its key.
I have an array of nearly sorted values 28 elements long. I need to
When I add values in NSMutableDictionary it automatically set Key wise. How can i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.