Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

programmatically re-order a collection of string values

Posted on 2009-04-07
3
Medium Priority
?
248 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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