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 aquarius with 2 posts (2 thanks)
    3. looks_3 gregid with 2 posts (3 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 2,141 views
    2. thumb_up 5 thanks given
    3. group 3 followers
    1. forum 9 posts
    2. attach_file 0 attachments




 
Search this Thread

Sorting a list in C#

  #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?
Deepmoney LLM
Elite Quantitative GenAI/LLM
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Better Renko Gaps
The Elite Circle
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Futures True Range Report
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
Battlestations: Show us your trading desks!
26 thanks
The Program
18 thanks
  #3 (permalink)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 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,162 since Jan 2013
Thanks Given: 57,341
Thanks Received: 26,267


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: 69

 
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: 650 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: 69


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


© 2024 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 - Privacy Policy - Downloads - Top
no new posts