Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

programmatically re-order a collection of string values

How would one go about changing the order of some string values in a collection.  Say I have the following list of values in a list collection:

List<string> colors = new List<string>("red","blue","yellow","green","purple");

I want to add the values in the colors list to a selection list, but in a different order:

"purple"
"yellow"
"green"
"blue"
"red"

Not ascending or descending, but in a sequence I need to specify.

How could I accomplish this?

Thanks for any help.
0
-Dman100-
Asked:
-Dman100-
  • 2
1 Solution
 
Anurag ThakurCommented:
kind of difficult when you dont have a proper sequence you want to display the colors
are they random numbers
if yes then you can generate a random number from 1 to 5 and then get the value out of the list
if the random no. is 3 then colors[3-1] will give you yellow

if there is one specific order then you might want to hardcode the values
0
 
-Dman100-Author Commented:
Hi ragi0017,

Thanks for replying to my post.  The values are returned and assigned to a list, which are not in the correct order.

There is one specific order that the values need to be in.

So, if I loop thru the list, how could I hardcode the values into the sequence that I need?

I tried using a if statement, but the values remained in the original order?  My logic was probably incorrect.

Thanks for your help.
0
 
Anurag ThakurCommented:
the best way will be to create a new list of colors and then add in them the names of colors in the order you need instead of trying to sort or reordering the existing list
it will save you time and effort
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now