StevieB
StevieB

Reputation: 6533

ASP.NET how to reverse List Array

Hey I have the following List

List<int> pos = new List<int>();

        for (int i = 0; i < combinedResults.Length; i++)
        {
            if (combinedResults[i])
            {
                pos.Add(i);
            }

        }

But It seems like the last array items are the ones I want first displayed so I was wondering how can I reverse the List ?

Upvotes: 2

Views: 1943

Answers (4)

derek
derek

Reputation: 4886

you could just add items to the list using Insert:

pos.Insert(0,i);

Upvotes: 0

Jack Marchetti
Jack Marchetti

Reputation: 15754

You can use the Reverse method.

Array.Reverse(arranyName);

Upvotes: 0

Tom Gullen
Tom Gullen

Reputation: 61727

Use the reverse method

pos.Reverse();

http://msdn.microsoft.com/en-us/library/b0axc2h2.aspx#Y400

Alternatively, loop in reverse, or even better, make sure the data is going in the correct way round and you wont have to maintain all these bits and bobs!

Upvotes: 2

sverre
sverre

Reputation: 6919

Run the loop in reverse:

for (int i = combinedResults.length-1; i >= 0; i--)

or reverse the list afterwards:

pos.Reverse()

Upvotes: 5

Related Questions