Solved

programmatically re-order a collection of string values

Posted on 2009-04-07
3
243 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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