# Sorting some numbers...kind of

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey

 std::list <unsigned int> Numbers; for (unsigned int i = 0; i < 100; i++) { Numbers.push_back(rand() % 9500); } 

Okay, so we have that piece of code. Right?

What I want to do now is to output all those numbers from lowest to highest.
Like, (31, 52, 60, 79) and not (85, 20, 852, 521)

So now you may be thinking: "Oh, that's just to swap the numbers and sort them like that."

But the thing is, I don't want to swap the numbers inside the list...
I want to simply just get the lowest number in the list like...
OutputNumbersFromLowestToHighest(Numbers);

And then it'd give me the answer. And again, I don't want anything in the Numbers list to change, I just want it read.

##### Share on other sites
Pass the list to the function by value, or pass it by reference and make an internal copy of it, and then sort it. You get the values in order, and the list is unchanged.

##### Share on other sites
I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

##### Share on other sites
Why not make a sorted copy of the list using a set (or a map if you want to print its index as well as the values in order)?

L. Spiro

##### Share on other sites
1. I love your name. YogurtEmperor.

2. If I understand correctly, you mean I should basically have a 2D list?
So I could get the index and number. I guess that makes sense.

Thanks. If anyone has anything else to add, do tell

##### Share on other sites

I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

I still don't see how sorting a copy won't work. If all you want is to output the numbers in order, how can you not sort a copy of the list and output it? The values come out in order, and the list is unchanged. Those are the only two requirements I can find in your posts.

##### Share on other sites

[quote name='Segun24' timestamp='1329132415' post='4912549']
I don't want the indexes to change at all. I want to simply output the numbers directly in order.
Because, when I use the sorted list, the indexes aren't the same... Get me?
I need each index to have their specific number.
I have a feeling recursion might be useful for this, but I can't come up with anything.

I still don't see how sorting a copy won't work. If all you want is to output the numbers in order, how can you not sort a copy of the list and output it? The values come out in order, and the list is unchanged. Those are the only two requirements I can find in your posts.
[/quote]

He wants for instance:

[1, 6, 2, 4]

to be 'outputted' like:

[1 (index 0), 2 (index 2), 4 (index 3), 6 (index 1)]

##### Share on other sites

int getNext(int n)
{
int candidate = inf;
foreach(value in list)
{
if(value <= n) continue;
if(value < candidate)
candidate = value;

}

return candidate;
}

And then:

int val = inf;
for(int i = 0; i < list.length(); ++i)
{

val = gentNext(val);
if(val == inf)
break; //no more numbers in list

print val;
}

Could most probably use some more work, but should work...

##### Share on other sites

2. If I understand correctly, you mean I should basically have a 2D list?
So I could get the index and number. I guess that makes sense.

No. 'copy list' != 'make 2D list'.

As two others already mentioned, the easiest way to solve this problem is to make a copy of the list, sort the copy (leaving the original unchanged), and print each item.

##### Share on other sites
That's correct, luca-deltodesco.

Thanks thok!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 23
• 11
• 25