Solved

programmatically re-order a collection of string values

Posted on 2009-04-07
3
238 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
Comment Utility
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-
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now