By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,310 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,310 IT Pros & Developers. It's quick & easy.

How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "

P: 1
I have looked All over the Internet to try and find an example of How to fix the following.

How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ?
Then Print out the Results in the format below.

I have enclosed my code and it works, but the problem happens when there are duplicate values,
which occurs when you use SortedList(). I would GREATLY APPRECIATE it if someone could PLEASE Modify this
Code or Show me EXACTLY how to do this another way, that is just as quick and efficent.

THANK YOU VERY MUCH in Advance.

BEFORE SORT:

VALUE's KEY's
sL.Add(1269.63,"white");
sL.Add(1270.36,"orange");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1272.06,"black");
sL.Add(1274.12,"dodBlue");
sL.Add(1276.02,"blue");
sL.Add(1273.21,"green");
sL.Add(1275.52,"red");

AFTER SORT:

VALUE's KEY's
sL.Add(1276.02,"blue");
sL.Add(1275.52,"red");
sL.Add(1274.12,"dodBlue");
sL.Add(1273.21,"green");
sL.Add(1272.06,"black");
sL.Add(1272.06,"yellow");
sL.Add(1271.50,"cyan");
sL.Add(1270.36,"orange");
sL.Add(1269.63,"white");

THEN PRINT OUT:

blue >= red ;
&& red >= dodBlue ;
&& dodBlue >= green ;
&& green >= yellow ;
&& yellow >= black ;
&& black >= cyan ;
&& cyan >= orange ;
&& orange >= white ;

*** For your information:
*** SMA is a Simple Moving Average Plotted on a Stock Charting Software Program.
*** (?) is the Period and [0] stands for the Current Bar.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Xml.Serialization;
  8. namespace Indicator
  9. {
  10.     public class SORT_SCRAP : Indicator
  11.     {/// This method is used to configure the indicator and is called once before any bar data is loaded.
  12.         protected override void Initialize()
  13.         {CalculateOnBarClose    = true;
  14.         Overlay    = true;}
  15.         /// Called on each bar update event (incoming tick)
  16.         protected override void OnBarUpdate()
  17.         {if ( CurrentBar < 200 )
  18.         return ;
  19.  
  20.         SortedList sL = new SortedList();
  21.  
  22.         sL.Add(SMA(8)[0], "white");
  23.         sL.Add(SMA(10)[0], "orange");
  24.         sL.Add(SMA(13)[0], "yellow");
  25.         sL.Add(SMA(20)[0], "cyan");
  26.         sL.Add(SMA(30)[0], "black");
  27.         sL.Add(SMA(40)[0], "dodBlue");
  28.         sL.Add(SMA(50)[0], "blue");
  29.         sL.Add(SMA(100)[0], "green");
  30.         sL.Add(SMA(200)[0], "red");
  31.  
  32.         Print("  " + " " + sL.GetByIndex(8) + " " + ">=" + " " + sL.GetByIndex(7));
  33.         Print("&&" + " " + sL.GetByIndex(7) + " " + ">=" + " " + sL.GetByIndex(6));
  34.         Print("&&" + " " + sL.GetByIndex(6) + " " + ">=" + " " + sL.GetByIndex(5));
  35.         Print("&&" + " " + sL.GetByIndex(5) + " " + ">=" + " " + sL.GetByIndex(4));
  36.         Print("&&" + " " + sL.GetByIndex(4) + " " + ">=" + " " + sL.GetByIndex(3));
  37.         Print("&&" + " " + sL.GetByIndex(3) + " " + ">=" + " " + sL.GetByIndex(2));
  38.         Print("&&" + " " + sL.GetByIndex(2) + " " + ">=" + " " + sL.GetByIndex(1));
  39.         Print("&&" + " " + sL.GetByIndex(1) + " " + ">=" + " " + sL.GetByIndex(0));
  40.         }
  41.     }
  42. }
Mar 15 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.