NexusFi: Find Your Edge


Home Menu

 





Sorting a list in C#


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one vantojo with 4 posts (0 thanks)
    2. looks_two gregid with 2 posts (3 thanks)
    3. looks_3 aquarius with 2 posts (2 thanks)
    4. looks_4 bobwest with 1 posts (0 thanks)
    1. trending_up 3,345 views
    2. thumb_up 5 thanks given
    3. group 3 followers
    1. forum 9 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

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.


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NYSE Owner ICE Invests in Crypto Exchange OKX at $25 Bil …
Cryptocurrency
Orban at 29pct as Hungary Votes Tomorrow -- McIlroy Surg …
Prediction Markets & Event Contracts
April 2026 Jobs Report: +115k vs +65k Expected
Traders Hideout
Saylors 41-Month HODL Breaks: Strategy Sells 32 BTC as $ …
Prediction Markets & Event Contracts
Iran Peace Expired NO: Ceasefire on Life Support, OPEC a …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
8 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623


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
Thanked by:
  #4 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

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


Started this thread Reply With Quote
  #5 (permalink)
 
bobwest's Avatar
 bobwest 
Western Florida
Site Moderator
 
Experience: Advanced
Platform: Sierra Chart
Trading: ES, YM
Frequency: Several times daily
Duration: Minutes
Posts: 8,174 since Jan 2013
Thanks Given: 57,989
Thanks Received: 26,406


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)
 
aquarius's Avatar
 aquarius 
Monterrey, Mexico
 
Experience: Beginner
Platform: NinjaTrader
Trading: Treasuries
Posts: 22 since Nov 2012
Thanks Given: 5
Thanks Received: 70

 
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; }


Reply With Quote
Thanked by:
  #7 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 649 since Aug 2009
Thanks Given: 320
Thanks Received: 623

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
Thanked by:
  #8 (permalink)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

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?


Started this thread Reply With Quote
  #9 (permalink)
 
aquarius's Avatar
 aquarius 
Monterrey, Mexico
 
Experience: Beginner
Platform: NinjaTrader
Trading: Treasuries
Posts: 22 since Nov 2012
Thanks Given: 5
Thanks Received: 70


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)
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012


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

and it works!!


Started this thread Reply With Quote




Last Updated on March 18, 2015


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts