Sorting a list in C# - NinjaTrader Programming | futures io social day trading
futures io futures trading


Sorting a list in C#
Updated: Views / Replies:593 / 9
Created: by vantojo Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Sorting a list in C#

  #1 (permalink)
Elite Member
Neuquen, Argentina
 
Futures Experience: Intermediate
Platform: Ninja
Favorite Futures: CL, TF, 6S, NG, sometimes GC
 
Posts: 195 since Jul 2012
Thanks: 25 given, 39 received

Sorting a list in C#

Sometimes I feel so stupid in C#, being from other coding paradigms....

None of the examples (array, arraylist, collection, etc) I've found through google show this simple need:

How do I make a list with two elements (Instrument, Range) which are types (string, int)

Reverse Sort the list by Range (highest first)

Then go through the list in the sort order, accessing both elements for reporting?

Thank you Guys.

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


If your issue is reverse order use Linq, add:
using System.Linq;

then eg.:
 
Code
foreach (var something in SomethingList.Reverse())
{
}
EDIT: Linq gives you much more methods to achieve what you need, eg. OrderBy, OrderByDescending, etc.

Reply With Quote
The following user says Thank You to gregid for this post:
 
  #4 (permalink)
Elite Member
Neuquen, Argentina
 
Futures Experience: Intermediate
Platform: Ninja
Favorite Futures: CL, TF, 6S, NG, sometimes GC
 
Posts: 195 since Jul 2012
Thanks: 25 given, 39 received

reply

mostly I need to know

- how to create (instantiate) the array or list so that it can contain both an integer and a string on the same "row"
- add "rows" to the list, each one with an integer and a string
- sort on the integer
- access the values

if someone can point me to the first two, then I can find the others

all the examples I've found only have one element in the array or list and sort on that one element, but that will not give me the capability I need

thank you

Reply With Quote
 
  #5 (permalink)
Market Wizard
Sarasota FL
 
Futures Experience: Intermediate
Platform: NinjaTrader, Sierra Chart
Favorite Futures: ES
 
Posts: 3,632 since Jan 2013
Thanks: 26,738 given, 11,106 received
Forum Reputation: Legendary


vantojo View Post
mostly I need to know

- how to create (instantiate) the array or list so that it can contain both an integer and a string on the same "row"
- add "rows" to the list, each one with an integer and a string
- sort on the integer
- access the values

if someone can point me to the first two, then I can find the others

all the examples I've found only have one element in the array or list and sort on that one element, but that will not give me the capability I need

thank you

Make it a List (or array) of either a class or a struct. The class or struct can have whatever elements you like.

Bob.

Reply With Quote
 
  #6 (permalink)
Elite Member
Monterrey, Mexico
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Treasuries
 
aquarius's Avatar
 
Posts: 22 since Nov 2012
Thanks: 5 given, 69 received

an idea...

 
Code
...

List<KeyValuePair<string,int> list = new List<KeyValuePair<string,int>>();
list.Add(new KeyValuePair<string,int>("abc", 1);

...
list.Sort(new Comparison<KeyValuePair<string, int>>((s, t) => s.Value.CompareTo(t.Value)));
list.Reverse();
...

int c= 0;
foreach(var v in list)
{ c+= v.Value; }


Last edited by aquarius; March 18th, 2015 at 12:42 PM. Reason: missed something...again
Reply With Quote
The following 2 users say Thank You to aquarius for this post:
 
  #7 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Take a look also at Dictionary or SortedList:
C# Dictionary Examples
C# SortedList Collection

these may be simpler for you to start with.

Reply With Quote
The following 2 users say Thank You to gregid for this post:
 
  #8 (permalink)
Elite Member
Neuquen, Argentina
 
Futures Experience: Intermediate
Platform: Ninja
Favorite Futures: CL, TF, 6S, NG, sometimes GC
 
Posts: 195 since Jul 2012
Thanks: 25 given, 39 received

to aquarius

thank you...that looks simple enough...

I have a question....

list.Sort(new Comparison<KeyValuePair<string, int>>((s, t) => s.Value.CompareTo(t.Value)));

does that sort the list by the integer?

Reply With Quote
 
  #9 (permalink)
Elite Member
Monterrey, Mexico
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Treasuries
 
aquarius's Avatar
 
Posts: 22 since Nov 2012
Thanks: 5 given, 69 received


vantojo View Post
thank you...that looks simple enough...

I have a question....

list.Sort(new Comparison<KeyValuePair<string, int>>((s, t) => s.Value.CompareTo(t.Value)));

does that sort the list by the integer?

Yes, it should. Any property (Key or Value) can serve as the sorting element for the collection, it depends which one you use. You can also exchange the KeyValuePair class for any other custom class in case you'd want to have more data stored in the object (just in case) for any further reference.

Reply With Quote
 
  #10 (permalink)
Elite Member
Neuquen, Argentina
 
Futures Experience: Intermediate
Platform: Ninja
Favorite Futures: CL, TF, 6S, NG, sometimes GC
 
Posts: 195 since Jul 2012
Thanks: 25 given, 39 received

to aquarias


thank you for your code snippet...it saved me a lot of frustration

and it works!!

Reply With Quote

Reply



futures io > > > > > Sorting a list in C#

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting futures contracts by relative strength stefan416 NinjaTrader 2 November 11th, 2013 10:14 AM
List.RemoveAt SerCava NinjaTrader Programming 5 June 16th, 2012 06:24 AM
Whats on your wish list incometrade Off-Topic 10 December 16th, 2011 01:28 PM
Sorting post by the number of thanks they received trendisyourfriend Off-Topic 1 May 9th, 2011 05:34 PM


All times are GMT -4. The time now is 01:24 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-11 in 0.14 seconds with 19 queries on phoenix via your IP 54.145.16.43