Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串的排序列表_C#_Sorting - Fatal编程技术网

C# 字符串的排序列表

C# 字符串的排序列表,c#,sorting,C#,Sorting,让我再问一次这个问题 假设数据库中有两个表,一个是任务,另一个是依赖项 任务表有一列(taskName),其中包含需要运行的所有任务 taskDependency表有两列,一列是tasks(TaskName),另一列是Dependencson 现在,假设我将任务表中的任务选择到一个列表中,并将依赖项表的依赖项选择到一个列表中。(由于某些任务具有多个依赖项,所以字典不起作用) 我如何将任务安排到正确的顺序 编辑 我在另一个论坛上得到了答案,我想我会在这里分享 你可以使用字典,你只需要明智地使用它

让我再问一次这个问题

假设数据库中有两个表,一个是任务,另一个是依赖项

任务表有一列(taskName),其中包含需要运行的所有任务

taskDependency表有两列,一列是tasks(TaskName),另一列是Dependencson

现在,假设我将任务表中的任务选择到一个列表中,并将依赖项表的依赖项选择到一个列表中。(由于某些任务具有多个依赖项,所以字典不起作用)

我如何将任务安排到正确的顺序

编辑

我在另一个论坛上得到了答案,我想我会在这里分享

你可以使用字典,你只需要明智地使用它

尝试字典>,其中键是任务名称,值是键所依赖的那些任务的任务名称集

为每个可能的任务预加载字典,其中包含一个空哈希集(表1)。 从数据库中更新它(表2) 创建空的输出列表 那么顺序是: 1.找到依赖项集为空的键,此组中没有顺序,将它们附加到输出列表中(并将它们从字典中删除)。 将这些单独列出。 1a。如果字典是空的,你就完了。 1b。如果没有找到空的依赖项集,那么您就有依赖项循环!失败。 2.对于字典中的所有剩余集合(即Dictionary.Values) 3.从集合中删除步骤1中找到的所有任务。 4.回到步骤1


CodeProject中的Thanx Mat不清楚您的数据结构(列表或数组)是什么,但是如果
list1
list
string[]
并且
list2
列表
list
string[][]
那么查询将是:

from str in list1
join pair in list2 on str = pair[0]
order by pair[1]
select str
static void Main(字符串[]args)
{
字典排序=新字典()
{
{“g”,“d”},{“s”,“e”},{“e”,“g”}
};
列表源=新列表(){“e”、“s”、“g”};
List result=source.OrderBy(key=>ordering[key]).ToList();
foreach(结果中的字符串s){Console.Write(s+“”);}
Console.WriteLine();
}

结果是
gse

一个向量排序,我们是根据一个向量对另一个向量排序?像这样的

// first, our base data
Tuple<char,char>[] data = { new Tuple<char,char>('g','d') ,
                            new Tuple<char,char>('s','e') , 
                            new Tuple<char,char>('d','s') , 
                            new Tuple<char,char>('e','g') , 
                          } ;

// initialize the two vectors
List<char>             keys   = new List<char>( data.Select(x => x.Item1 ) ) ; // {g,d,s,e}
List<Tuple<char,char>> values = new List<Tuple<char, char>>( data ) ;

// An in-place sort of the "keys" vector in terms of the payload of the "values" vector
keys.Sort( (x,y) => {

  // first, find the associated value for each key (if such exists)
  char? xValue = null ;
  char? yValue = null ;
  foreach (Tuple<char, char> t in values)
  {
    if ( t.Item1 == x && !xValue.HasValue ) xValue = t.Item2 ;
    if ( t.Item1 == y && !yValue.HasValue ) yValue = t.Item2 ;
    // if we found both values, we can quit                 
    if ( xValue.HasValue && yValue.HasValue  ) break ;
  }

  // set the condition (comparison code)
  int cc ;
  if      ( xValue.HasValue && yValue.HasValue ) cc =  xValue.Value.CompareTo( yValue.Value) ;
  else if ( xValue.HasValue                    ) cc = +1 ; // treat NULL as collating lower than non-null
  else if (                    yValue.HasValue ) cc = -1 ; // treat NULL as collating lower than non-null
  else                                           cc =  0 ; // equal if both are NULL

  // return it
  return cc ;
});
//首先,我们的基础数据
Tuple[]data={new Tuple('g','d'),
新元组('s','e'),
新元组('d','s'),
新元组('e','g'),
} ;
//初始化两个向量
列表键=新列表(data.Select(x=>x.Item1));//{g,d,s,e}
列表值=新列表(数据);
//根据“值”向量的有效载荷,一种就地排序的“键”向量
关键字排序((x,y)=>{
//首先,找到每个键的关联值(如果存在)
char?xValue=null;
char?yValue=null;
foreach(值中的元组t)
{
如果(t.Item1==x&&!xValue.HasValue)xValue=t.Item2;
如果(t.Item1==y&&!yValue.HasValue)yValue=t.Item2;
//如果我们找到了这两个值,我们可以退出
如果(xValue.HasValue&&yValue.HasValue)中断;
}
//设置条件(比较代码)
int cc;
如果(xValue.HasValue&&yValue.HasValue)cc=xValue.Value.CompareTo(yValue.Value);
else if(xValue.HasValue)cc=+1;//将NULL视为低于非NULL的排序规则
else if(yValue.HasValue)cc=-1;//将NULL视为排序规则低于非NULL
else cc=0;//如果两者都为NULL,则相等
//还它
返回cc;
});

使用哪种语言?根据清单2排序?不清楚您想要什么。举个例子。排序后,您希望列表1的外观如何。两个容器中的元素数量是否固定?列表2的类型是什么??如果一个项目依赖于多个值/然后更改OrderBy的值怎么办!但是一本字典有两个相同的键,因为字典是无关紧要的。重要的是,您正在使用另一个字符串对一个字符串列表进行排序,而该字符串来自其他地方(在本例中为字典),这或多或少是您要求的,对吗?这是正确的,我们一直试图采用该逻辑并将其应用于我的senario,但没有任何效果
// first, our base data
Tuple<char,char>[] data = { new Tuple<char,char>('g','d') ,
                            new Tuple<char,char>('s','e') , 
                            new Tuple<char,char>('d','s') , 
                            new Tuple<char,char>('e','g') , 
                          } ;

// initialize the two vectors
List<char>             keys   = new List<char>( data.Select(x => x.Item1 ) ) ; // {g,d,s,e}
List<Tuple<char,char>> values = new List<Tuple<char, char>>( data ) ;

// An in-place sort of the "keys" vector in terms of the payload of the "values" vector
keys.Sort( (x,y) => {

  // first, find the associated value for each key (if such exists)
  char? xValue = null ;
  char? yValue = null ;
  foreach (Tuple<char, char> t in values)
  {
    if ( t.Item1 == x && !xValue.HasValue ) xValue = t.Item2 ;
    if ( t.Item1 == y && !yValue.HasValue ) yValue = t.Item2 ;
    // if we found both values, we can quit                 
    if ( xValue.HasValue && yValue.HasValue  ) break ;
  }

  // set the condition (comparison code)
  int cc ;
  if      ( xValue.HasValue && yValue.HasValue ) cc =  xValue.Value.CompareTo( yValue.Value) ;
  else if ( xValue.HasValue                    ) cc = +1 ; // treat NULL as collating lower than non-null
  else if (                    yValue.HasValue ) cc = -1 ; // treat NULL as collating lower than non-null
  else                                           cc =  0 ; // equal if both are NULL

  // return it
  return cc ;
});