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

  • SEARCH
  • Home
  • 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 7586933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:32:57+00:00 2026-05-30T19:32:57+00:00

Here is a example of what I want to do with NSArray contains NSNumber.

  • 0

Here is a example of what I want to do with NSArray contains NSNumber.

  1. This is the NSArray “score” I want to edit.
    score[0]=30,
    score[1]=10,
    score[2]=20
    score[3]=0

  2. Sort the Array in ascending-order
    score[0]=30 //[0]This number shows the index before sorting the array
    score[1]=20 //[2]
    score[2]=10 //[1]
    score[3]=0 //[3]

  3. Edit the Array(In this case,4th gives 1st 10 points,and 3rd gives 2nd 5points)
    score[0]=40 //[0]
    score[1]=25 //[2]
    score[2]=5 //[1]
    score[3]=-10 //[3]

  4. And sort the array back they were.
    score[0]=40
    score[1]=5
    score[2]=25
    score[3]=-10

    I have a problem with no.4 method in the list.Can someone give me some idea with this?
    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-05-30T19:32:58+00:00Added an answer on May 30, 2026 at 7:32 pm

    You don’t actually need to swap the values themselves, you can set up an extra level of indirection and use that.

    Before the sort, you have the indexes initialised to point to the corresponding scores:

    index[0] = 0      score[0] = 30
    index[1] = 1      score[1] = 10
    index[2] = 2      score[2] = 20
    index[3] = 3      score[3] =  0
    

    When you sort, you actually sort the indexes based on the scores they point to rather than the scores themselves. So, instead of the following comparison in your sort:

    if score[i] > score[i+1] then swap score[i], score[i+1]
    

    you instead use:

    if score[index[i]] > score[index[i+1]] then swap index[i], index[i+1]
    

    Following the sort, you then have:

    index[0] = 0      score[0] = 30
    index[1] = 2      score[1] = 10   \ These two indexes have been swapped
    index[2] = 1      score[2] = 20   /   but NOT the scores.
    index[3] = 3      score[3] =  0
    

    Then, to move points, you use the indirect indexes rather than the direct values:

    score[index[0]] += 10; score[index[3]] -= 10;
    score[index[1]] +=  5; score[index[2]] -=  5;
    

    Then you throw away the indexes altogether, the original array doesn’t need restoration to its original order, simply because its order was never changed.

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

Sidebar

Related Questions

See array for example: here Basically, I want to find the max() for array[]['time']
I want to compare an array containing int values to a number example [1,2,30,1,40,200,10,500]
I want that find empty tags, here is a example txt =<lol1><><lol2> rgx =
Here is a complete example. I want to forbid using A::set from objects casted
ex: <a><strike>example data in here</strike></a> I want everything inside the a tag, to the
The fictional example here. Suppose I designed a class named IODevice. This class already
This problem is not readily reproducible in a simple example here but was wondering
What does this do? I find the example here, but other than what it
Here an example my wordpress post. I want to add some class to the
I need to generate a sorted array of NSNumber s from another NSArray .

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.