Solved

programmatically re-order a collection of string values

Posted on 2009-04-07
3
241 Views
Last Modified: 2012-05-06
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
Comment
Question by:-Dman100-
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24088679
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
 

Author Comment

by:-Dman100-
ID: 24088789
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
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 24093867
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question