Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.

C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
namespace Indicator
{
    public class SORT_SCRAP : Indicator
    {/// This method is used to configure the indicator and is called once before any bar data is loaded.
        protected override void Initialize()
        {CalculateOnBarClose    = true;
        Overlay = true;}
        /// Called on each bar update event (incoming tick)
        protected override void OnBarUpdate()
        {if ( CurrentBar < 200 )
        return ;

        SortedList sL = new SortedList();

        sL.Add(SMA(8)[0], "white");
        sL.Add(SMA(10)[0], "orange");
        sL.Add(SMA(13)[0], "yellow");
        sL.Add(SMA(20)[0], "cyan");
        sL.Add(SMA(30)[0], "black");
        sL.Add(SMA(40)[0], "dodBlue");
        sL.Add(SMA(50)[0], "blue");
        sL.Add(SMA(100)[0], "green");
        sL.Add(SMA(200)[0], "red");

        Print("  " + " " + sL.GetByIndex(8) + " " + ">=" + " " + sL.GetByIndex(7));
        Print("&&" + " " + sL.GetByIndex(7) + " " + ">=" + " " + sL.GetByIndex(6));
        Print("&&" + " " + sL.GetByIndex(6) + " " + ">=" + " " + sL.GetByIndex(5));
        Print("&&" + " " + sL.GetByIndex(5) + " " + ">=" + " " + sL.GetByIndex(4));
        Print("&&" + " " + sL.GetByIndex(4) + " " + ">=" + " " + sL.GetByIndex(3));
        Print("&&" + " " + sL.GetByIndex(3) + " " + ">=" + " " + sL.GetByIndex(2));
        Print("&&" + " " + sL.GetByIndex(2) + " " + ">=" + " " + sL.GetByIndex(1));
        Print("&&" + " " + sL.GetByIndex(1) + " " + ">=" + " " + sL.GetByIndex(0));
        }
    }
}


[edit]Code blocks sorted - OriginalGriff[/edit]
Posted
Updated 14-Mar-11 21:53pm
v2
Comments
_Maxxx_ 15-Mar-11 20:43pm    
IN your example, shouldn't green >= black, black >= yellow rather than the other way around?
Also your Key and Value column headings are reversed - which may cause confusion - the SortedList.Add method takes a Key followed by a Value.

Off the top of my head, the easiest way would store the colour name in the SMA object (or 'enclose' both of them in a parent object) and then implement an IComparer to compare the two objects, taking into account both the numeric and string values.

e.g.
public class MySortingClass
{
public decimal numberbit{get;set;)
public string stringbit{get;set;)
}


then
public class MyComaringClass : IComparer
{
    int compare(object a, object b)
    {
        MySortingClass A = a as MySortingClass;
        MySortingClass B  = b as MySortingClass;

        if (A.numberbit < B.numberbit) return -1;
        if (A.numberbit > B.numberbit) return 1;

        if (A.stringbit > B.stringbit) return -1;
        if (A.stringbit < B.stringbit) return 1;

        return 0
    }
}


then you instantiate your Sortedlist passing the Comparer into the constructor, add your MySortingClass objects to the sortedlist and Bob should be your Uncle...
 
Share this answer
 
The Following code worked but I can't get it to Print Correctly:

using System;
using System.Collections.Generic;
using System.Linq;

var list = new List<KeyValuePair<double,string>>();

list.Add(new KeyValuePair<double, string>(LinReg(8)[0],"white"));
list.Add(new KeyValuePair<double, string>(LinReg(10)[0],"orange"));
list.Add(new KeyValuePair<double, string>(LinReg(13)[0],"yellow"));
list.Add(new KeyValuePair<double, string>(LinReg(20)[0],"cyan"));
list.Add(new KeyValuePair<double, string>(LinReg(30)[0],"black"));
list.Add(new KeyValuePair<double, string>(LinReg(40)[0],"dodBlue"));
list.Add(new KeyValuePair<double, string>(LinReg(50)[0],"blue"));
list.Add(new KeyValuePair<double, string>(LinReg(100)[0],"green"));
list.Add(new KeyValuePair<double, string>(LinReg(200)[0],"red"));

var query = from kvp in list orderby kvp.Key descending, kvp.Value
select new KeyValuePair<double, string>(kvp.Key, kvp.Value);

list = query.ToList();

When it was sorted the following ORDERED RESULT Occured:

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

My software does not recognize the Console.WriteLine command.

It uses the Print() command to Print to the OutPut Window of the software.

Could you Please tell me how I can access the Values in the sorted List so I can print them out accordingly?

Print(pair.Value(0) + " " + ">=" + " " + pair.Value(1) + ";") <------- ???

Print("&&" + " " + pair.Value(1) + " " + ">=" + " " + pair.Value(2) + ";") <------- ???

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

Print("&&" + " " + pair.Value(6) + " " + ">=" + " " + pair.Value(7) + ";") <-------- ???

Print("&&" + " " + pair.Value(7) + " " + ">=" + " " + pair.Value(8) + ";") <-------- ???

So the Print out looks Like the Following:

blue >= red ;

&& red >= dodBlue ;

&& dodBlue >= green ;

&& green >= yellow ;

&& yellow >= black ;

&& black >= cyan ;

&& cyan >= orange ;

&& orange >= white ;

THANK YOU VERY MUCH IN ADVANCE.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900