如何按“值"以降序对列表排序?并按“键"对重复项进行排序? [英] How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ?

查看:77
本文介绍了如何按“值"以降序对列表排序?并按“键"对重复项进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Internet上寻找了如何解决以下问题的示例.

如何按值"以降序对列表进行排序,并按键"对重复项进行排序?然后以以下格式打印结果.

我已经封闭了我的代码,并且可以正常工作,但是当存在重复的值时就会发生问题,当您使用SortedList()时就会发生这种情况.如果有人可以修改此代码或向我确切演示如何以另一种方式来做到这一点,那将是我的荣幸,这同样快捷有效.

提前非常感谢您.

排序之前:

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 ;



***供您参考:
*** SMA是绘制在股票图表软件程序上的简单移动平均线.
***(?)是时间段,[0]代表当前柱线.



*** 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.

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]已排序的代码块-OriginalGriff [/edit]



[edit]Code blocks sorted - OriginalGriff[/edit]

推荐答案

最简单的方法是将颜色名称存储在SMA对象中(或将两个都封闭"在父对象中),然后实现IComparer来比较两个对象,同时考虑数字和字符串值.

例如
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
    }
}



然后实例化将Comparer传递到构造函数中的Sortedlist,将MySortingClass对象添加到sortedlist中,而Bob应该是您的叔叔...



then you instantiate your Sortedlist passing the Comparer into the constructor, add your MySortingClass objects to the sortedlist and Bob should be your Uncle...


以下代码有效,但我无法获取正确打印:

使用系统;
使用System.Collections.Generic;
使用System.Linq;

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

list.Add(新的KeyValuePair< double,字符串>(LinReg(8)[0],"white")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(10)[0],"orange")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(13)[0],"yellow")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(20)[0],"cyan")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(30)[0],"black")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(40)[0],``dodBlue'')));
list.Add(新的KeyValuePair< double,字符串>(LinReg(50)[0],"blue")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(100)[0],绿色")));
list.Add(新的KeyValuePair< double,字符串>(LinReg(200)[0],"red")));

var query =来自kvp的列表顺序,按kvp.Key降序,kvp.Value
选择新的KeyValuePair< double,字符串>(kvp.Key,kvp.Value);

list = query.ToList();

排序后,会出现以下ORDERED RESULT:

VALUE的密钥
(1276.02,蓝色")
(1275.52,红色")
(1274.12,"dodBlue")
(1273.21,绿色")
(1272.06,黄色")
(1272.06,黑色")
(1271.50,青色")
(1270.36,橙色")
(1269.63,白色")

我的软件无法识别Console.WriteLine命令.

它使用Print()命令打印到软件的输出窗口.

您能否告诉我如何访问已排序列表中的值,以便相应地将其打印出来?

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

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

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

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

vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

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

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

因此,打印输出如下所示:

蓝色> =红色;

&&红色> = dodBlue;

&& dodBlue> =绿色;

&&绿色> =黄色;

&&黄色> =黑色;

&&黑色> =青色;

&&青色> =橙色;

&&橙色> =白色;

非常感谢.
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.


这篇关于如何按“值"以降序对列表排序?并按“键"对重复项进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆