排序字符串列表 [英] Sorting list of string

查看:108
本文介绍了排序字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我再问这个问题



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



任务表有一列(taskName),需要运行所有任务



taskDependency表有2列,一列是任务(taskNames)和其他依赖项(taskDependsOn)



现在说我从任务表中选择任务到列表中,并将依赖项表的依赖项放入List中。 (字典不会起作用,因为某些任务有多个依赖关系)



我如何按正确的顺序完成任务?

Let me re ask this question

Say I have two tables in a database one being tasks and the other being Dependencies

The task table has one column (taskName) with all the tasks that needs to be run

The taskDependency table has 2 columns one being tasks (taskNames) and the other dependencies (taskDependsOn)

Now say I select the tasks from the task table into a list and the dependencies for the dependencies table into a List.(Dictionary wont work as some tasks has multiple dependencies)

How would I get the tasks into the right order?

推荐答案

可以使用字典,你必须明智地使用



尝试字典<字符串,HashSet< string>> 其中键是任务名称,值是集合密钥所依赖的任务的任务名称。



为每个可能的任务(表1)预加载带有空HashSet的Dictionary。

从DB更新它(表2)

创建空输出列表

然后顺序是:

1.找到具有空依赖集的键,该组中没有顺序,将这些顺序附加到输出列表(并从字典中删除它们。

保留一份单独的列表。

1a。如果字典是空的,你就完成了。

1b。如果没有找到空的依赖集,那么你有依赖循环!失败。

2.对于词典中的所有剩余集合(即,词典。值)

3.从集合中删除步骤中找到的所有任务1.

4.回到第1步。
You can use a Dictionary, you just have to use it wisely!

Try a Dictionary<string, HashSet<string>> where the key is a task name and the value is the set of task names of those tasks upon which the key depends.

Preload the Dictionary with an empty HashSet for every possible task (table 1).
Update it from the DB (table 2)
Create the empty output List
Then the order is:
1. find the keys with empty dependency sets, there is no order in this group, append these to the output list (and remove them from the Dictionary).
keep a separate list of these.
1a. if the dictionary is empty, you're done.
1b. if no empty dependency sets are found, then you have dependency cycles! FAIL.
2. for all of the remaining sets in the Dictionary (i.e., the dictionary.Values)
3. remove from the set all of the tasks found in step 1.
4. back to step 1.


这篇关于排序字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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